First, I'd like to clarify that following notations have different semantics:
``
val method : () -> Unit ? // function without parameters returning Unit
val method : (() -> Unit )? // nullable reference to function without parameters returning Unit
If compiler doesn’t show any meaningful error message, but crashes with exception, it means that it contains a bug. Feel free to report it directly to Kotlin issue tracker. I have reported your problem: KT-1649.
Also, you have an error in your code: automatic typecasting from nullable to not-null value happens only for local variables. It can’t happen for properties, because declaring that property is “val” doesn’t guarantee that it will have the same value every time. It only means that no value can be assigned to this property. Therefore, to make your code work, you should save a.method to local variable and check it for null:
``
trait A {
val method : (() -> Unit )?
}
fun test(a : A) {
val method = a.method
if (method != null) {
method()
}
}
Actually, compiler should complain when you try to invoke nullable function reference, it’s a known issue which is going to be fixed soon: KT-1264