DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Discussion on: Can someone explain this Kotlin expression.

Collapse
 
jmfayard profile image
Jean-Michel Fayard πŸ‡«πŸ‡·πŸ‡©πŸ‡ͺπŸ‡¬πŸ‡§πŸ‡ͺπŸ‡ΈπŸ‡¨πŸ‡΄

Do I say, variable callbackFunction is of type Boolean, Or of type Unit
In that abstract function, is callback type of Boolean or Unit or something else?

The variable can be assigned a function that takes a boolean and returns Unit. The function takes as a parameter a function that takes a boolean and returns Unit

Does it mean variable is initialized by an empty anonyms function

{} is function that do nothing

Here you go:

Collapse
 
abhinav1217 profile image
Abhinav Kulshreshtha Author

So callbackFunction is not a variable as such, it's a fancy way of declaring a function which can be defined later, and that function has a boolean parameter, and returns a unit value. Am i Getting this right?

Collapse
 
jmfayard profile image
Jean-Michel Fayard πŸ‡«πŸ‡·πŸ‡©πŸ‡ͺπŸ‡¬πŸ‡§πŸ‡ͺπŸ‡ΈπŸ‡¨πŸ‡΄ • Edited on

Not yet right :)

callbackFunction is really a variable, but what kind of value does it accept?
Answer: the value you assign to that variable is a function with a boolean argument.

The basic principle is that, like in Javascript, functions are first-class citizens that you can pass in a parameter of a function or in a variable. To do the same thing In Java, you would have to define a meaning-less interface with one function.

You can read the friendly documentation at kotlinlang.org/docs/reference/lamb...

Thread Thread
 
abhinav1217 profile image
Abhinav Kulshreshtha Author

Thanks.. I was actually reading through documentation right now. But your explanation is so much simpler to understand. :)

I also asked this on StackOverflow and your answer along with explanation on internal working from SO, made lambdas so much clear to me.

Thread Thread
 
jmfayard profile image
Jean-Michel Fayard πŸ‡«πŸ‡·πŸ‡©πŸ‡ͺπŸ‡¬πŸ‡§πŸ‡ͺπŸ‡ΈπŸ‡¨πŸ‡΄

Glad I could help, don't hesitate asking other #explainmelikeim5 Kotlin questions. I like the exercise of explaining useful things in simple words!

Thread Thread
 
jmfayard profile image
Jean-Michel Fayard πŸ‡«πŸ‡·πŸ‡©πŸ‡ͺπŸ‡¬πŸ‡§πŸ‡ͺπŸ‡ΈπŸ‡¨πŸ‡΄