I am learning android development using kotlin. Being primarily a web developer, I am new to both technology.
While looking for API-29 Connectivity Solution (NetworkInfo
and getActiveNetworkInfo
are deprecated in API-29
), I ran across this expression for variable declaration.
protected var callbackFunction: ((Boolean) -> Unit) = {}
abstract fun startListening(callback: (Boolean) -> Unit)
How would I interpret this? Do I say, variable callbackFunction is of type Boolean, Or of type Unit
. Or something else. In that abstract function, is callback type of Boolean or Unit or something else?
Also I am a bit confused, about the curly braces. Does it mean variable is initialized by an empty anonyms function ( something like in javascript ) or is it something entirely different concept?
Top comments (10)
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
{}
is function that do nothingHere you go:
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?
Not yet right :)
callbackFunction
is really avariable
, but what kind of value does it accept?Answer: the
value
you assign to that variable is afunction 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...
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.
Glad I could help, don't hesitate asking other #explainmelikeim5 Kotlin questions. I like the exercise of explaining useful things in simple words!
Follow-up:
Send me all the Kotlin questions you were too afraid to ask
Jean-Michel Fayard γ» Oct 6 γ» 1 min read
This variable is of type "function". In Kotlin, functions are first-class citizens, that means, you can use functions just like other variables --- pass functions as parameters to other functions, store functions in variables etc.
You can read that expression like this: "The variable callbackFunction is of type Function --- that accepts a Boolean and returns a Unit."
Later you can assign a function to the variable like this:
...or this:
Also, the variable is initialized by assigning an anonymous function like
{}
.Special thanks for assign by reference example. It is a nice trick which I should use in future codes.
It's a lambda function that's the callback, it gives you back a boolean value whilst the function itself is Unit.
So somewhere in your code you'll have
startListening { it // this it will be the boolean returned from the callback
}
For more clarification read high order functions in Kotlin
Will read about them again. Thanks.
I have hated lambdas in every programming languages. Seems like they are unavoidable these days.