Lambdas

We've already seen how Kotlin can use the object keyword to create an anonymous instance of a class. This can be used when we want to pass some functionality as an argument to another method. But if the interface or a class you are creating has only one function, then using anonymous classes may feel cumbersome and too verbose.

This is where lambda expressions, or lambdas for short, come into play. Lambdas can be best described as short pieces of code that can be passed to other functions. If you have a Java background, then you probably remember how Java 8 was highly anticipated because it finally brought support for lambdas.

In UI programming, event listeners are the perfect use case for lambdas. We're going to take a look at an example from Android. Android still uses Java 6, and there we can't use Java lambdas, so it will be perfect to show how using Kotlin instead of Java is less verbose if you can't use the latest Java versions. On android with Java, you would set a click listener on a button object like this:

public void setClickListener() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.print("Clicked on: " + v.toString());
}
});
}

All view classes in android have the setOnClickListener method (which they inherit from the View base class), which accepts as an argument an instance of the View.OnClickListener interface. Most of the time, you'd pass this argument by creating an instance with an anonymous class on the fly, like we did in the previous example.

The same code in Kotlin, thanks to lambdas, can be expressed with one line only:

button.setOnClickListener({ v: View -> println("Clicked on: $v") })

Now, let's take a look at the code of the View.OnClickListener interface from the android source code, as it will help us with explaining the lambda syntax:

/**
* Interface definition for a callback to be invoked when a view is clicked.
*/
public interface OnClickListener
{
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
void onClick(View v);
}

The interface has only one method, onClick, and this is the method we've declared with the Kotlin lambda.

Lambda functions in Kotlin are always declared inside the curly braces. Inside the curly braces, first we declare the parameters of our lambda; our onClick method accepts one, a View type. If there were more, they'd be separated by commas. Note that parameters are not enclosed inside parentheses.

After the parameters comes the arrow (->) and then the lambda body. You can think of this arrow as trying to say that parameters from the left are passed to the body on the right.

Type inference also works on lambda parameters; you don't have to explicitly specify that the v parameter is of View type:

button.setOnClickListener { v -> println("Clicked on: $v") }

Lambdas don't have to be one-liners like the previous example. You can also declare them with multiple lines inside the body. Here's how we would define a lambda that sums two integers with a multiline body:

{  
a: Int, b: Int ->
println("Summing numbers: $a and $b")
val result = a + b
println("The result is: $result")
result
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.234.150