Published on

Lambda and SAM Conversion in Kotlin - Part 1

Authors

I love lambda expression. Whether it's in Java, Kotlin, or Javascript (arrow function). It helps me remove verbosity from my code, for example:

Given a View i want to listen to a click event from user, in this case we want to make the listener to be an interface named OnClickListener with single access method (SAM)

So the listener will be like this:

public interface OnClickListener{
  void onClick(View view);
}

Then i want to bind my view with the listener, without lambda it will go like this:

view.setOnClickListener(new OnClickListener {
  @Override public void onClick(View view){
    // do something
  }
}

Even worse, in Kotlin without lambda

view.setOnClickListener(object: OnClickListener{
  fun void onClick(view: View){
    // do something
  }
}
4 ~ 5 LOC for listening to user click only

Luckily, in Java 8 you can convert that SAM interface to lambda, and if you targeting Java 7 below you can use Retrolambda! Here's the code after we apply lambda expression

view.setOnClickListener(view -> // do something with `view`)

So how about Kotlin?

If your interface is written in Java, Kotlin automagically will convert your SAM to lambda expression just like Java 8 lambda specification

view.setOnClickListener { // do something with `it }
If the parameter is singular you can use it keyword. More on this

Wait what? That's right folks, it only convert the interface written in Java.

And if you don't want to use lambda adapter function, the method with the interface parameter must written in Java too.

view.setOnClickListener( OnClickListener {
  // do something with `it`
})
Example of adapter function in lambda. Originally designed to handle multiple method taking functional interface

-- So, how we can implement lambda and or SAM conversion in Kotlin?

Let's discussed it in another time 👋

Cao ~