Paweł Byszewski
Paweł Byszewski

Reputation: 420

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?

Upvotes: 28

Views: 26515

Answers (2)

Catluc
Catluc

Reputation: 1823

Look at the last parameter of the exposed function:

internal fun showDialog(title: String, message: String? = null, okButtonAction: () -> Unit, koButtonAction:() -> Unit = {}) { //do things..... }

Upvotes: 15

yole
yole

Reputation: 97148

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.

Upvotes: 16

Related Questions