Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null or empty lambda as default value

Which solution is better? Using a nullable lambda or passing an empty lambda as a default parameter? Would kotlin somehow optimize empty lambda, or create a new instance that does nothing?

class Test1(val action: () -> Unit = {})

Unfortunately, I do not understand generated byte code. Let's analyze

val test11 = Test1()

After decompilation gives us:

private static final Test1 test11 = new Test1((Function0)null, 1, (DefaultConstructorMarker)null);

And finally, as a lambda is passed, something like this:

var1 = (Function0)null.INSTANCE;

Edit: The hidden questions is: How does Kotlin treat an empty lambda as a default value?

like image 631
Paweł Byszewski Avatar asked Aug 05 '16 08:08

Paweł Byszewski


People also ask

Which has a default value of NULL Java?

Reference Variable value: Any reference variable in Java has a default value null.

Why default value of string is null in Java?

Your String reference "a" is not referring to anything here, hence it is null (following the default behavior of Object references of Java). And the default value of an String object (unless you give it a value while instantiating) is the "empty" String.

How does Lambda work in Kotlin?

A lambda expression is a shorter way of describing a function. It doesn't need a name or a return statement. You can store lambda expressions in a variable and execute them as regular functions. They can also be passed as parameters to other functions or be the return value.


1 Answers

It is definitely more idiomatic to pass an empty lambda rather than null as a default value for a lambda parameter.

The decompiler used in IntelliJ IDEA does not always handle Kotlin bytecode particularly well, so what you see in its output in this case does not reflect what actually happens. In reality, the empty lambda will be compiled to a singleton nested class implementing the corresponding FunctionN interface with an empty body, and the singleton instance will be used as the default value.

See my talk slides for more information on how default parameters are implemented in Kotlin.

like image 146
yole Avatar answered Sep 17 '22 11:09

yole