I have a few confused questions.
Main question:
ViewSwitcher.ViewFactory
or RecyclerView.Adapter
.
What is those? Isn’t it nested class? Why aren’t there parentheses? Could you share simple Kotlin examples with me about those?
First question:
I learned the setFactory()
that its the SAM. But isnt SAM the Single Abstract Method? Take a look that.
I saw the setFactory() not single method. There are many methods.
Second question :
Why is setFactory()
used like a higher order function? Why setFactory{ code }
. Why is not setFactory(){code}
I’m confused every time I look at Android References.
Yes your questions are quite confused
They are nested types in Android, one is an interface and the other an abstract class.
In what context? Would never make sense to put parentheses after an interface as you cannot create an instance of one. It would make sense to use parentheses when subclassing the abstract class but that is the only place as in:
class MyAdapter(): RecyclerView.Adapter<Foo>() {
This is a Kotlin forum not an Android one so your request is not appropriate.
Presuming you are referring to ViewSwitcher.setFactory, it is not a SAM. It has a parameter of ViewSwitcher.ViewFactory which is an interface that does describe a single abstract method thus ViewSwitcher.ViewFactory is a SAM. Since the parameter to setFactory is a SAM you can pass a lambda and it will be converted to the SAM type automatically.
Not sure what you are referring to since there is only ViewSwitcher.setFactory method. But the method that the SAM is passed to does not matter it is the type of the parameter that matters on SAM conversion.
“A higher-order function is a function that takes functions as parameters, or returns a function.”
In this case it takes a SAM as a parameter, which also qualifies as a function, thus setFactory actually is a higher order function.
Both are valid syntax. Kotlin allows you to drop the parentheses when the only thing you pass to the function is a lambda.
Actually I was ask that why not used the parantheses on ViewSwitcher.ViewFactory
or RecyclerView.Adapter
. Maybe I understood wrong. I need the paranthese when I do try like the example. Why not used the parantheses in this code?
Maybe show a short example of your attempt?
That’s documentation, not code.
I guess I was able. Is this example suitable for what I’m talking about?
Yes, you have right but I was talking about the ViewSwitcher.ViewFactory code.
Not really. Please specify which pair of parentheses is confusing you.
There is zero code on that page of documentation. I have no idea what code you are refering to.