Optional function parameters


#1

I want to declare a function that takes an optional callback. Right now I am using:

fun foo(callback: () -> Unit = { /* nop */ } ) { … }

I assume the compiler would translate the empty function to empty private method, which can be optimized by the JIT. Is that really the case?

Just out of curiosity, Is there a syntax to specify nullable function type?


#2

Unfortunately, it will be compiled to invocation of function class with empty implementation. We are planning to implement function inlining later, which will compile your code in more effective way.

Function type surely can be nullable, here is an example:

``

fun foo(callback: (() -> Unit)? = null) { … }


#3

Eventually, we will use method handles to implement lambda expression, but so far we just create a new (anonymous) class for every lambda expression


#4

Forgive me if there is an obvious answer, but has the situation on this changed? Can we pass optional lambda parameters like we can with param: String?? Kotlin syntax is usually beautiful; fun foo(() -> Unit = {}) isn’t the best.