Here is a class similar to one I have:
class Test(
private val testInvokeOperator: (() -> Unit)? = null
) {
fun test() {
if (testInvokeOperator != null) {
testInvokeOperator()
}
}
}
I expect to be able to use the invoke operator on the lambda since it’s smart-casted to a non-nullable type, but the compiler gives me an error saying “Only safe and non-null asserted calls are allowed…” (the one you get when trying to call a method on a null reference).
If I add a non-null assertion (!!) I get a warning that the assertion is unnecessary.
Everything works as expected if I replace the operator with the invoke method call (no need to perform a null-safe call).
I use:
Kotlin 1.3.72
Android Studio 3.6.3
AGP 3.5.3