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?
Reference Variable value: Any reference variable in Java has a default value null.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With