Can someone explain this Kotlin expression.

twitter logo ・1 min read

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?

twitter logo DISCUSS (10)
markdown guide
 

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:

 

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 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...

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.

 
protected var callbackFunction: ((Boolean) -> Unit) = {}

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:

callbackFunction = { b: Boolean ->
    println("The value is a $b")
}

...or this:

fun showResults(b: Boolean){
    println("The value is $b")
}

callbackFunction = ::showResults   // assign by reference

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.

Classic DEV Post from Mar 9

What’s an unpopular software opinion you have?

Please share! ...

Abhinav Kulshreshtha profile image