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()
反编译后,我们得到:
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;
编辑: