Smartcasting a function type property seems to not...
# announcements
f
Smartcasting a function type property seems to not work.
Copy code
class Foo(val callback: (() -> Unit)? = null) {
    init {
        if (callback != null) {
            callback() // Error: Reference has a nullable type '(() -> Unit)?'
        }
    }
}
This works fine:
Copy code
class Foo(callback: (() -> Unit)? = null) {
    init {
        if (callback != null) {
            callback() // Error: Reference has a nullable type '(() -> Unit)?'
        }
    }
}
And this too:
Copy code
class Foo(val callback: (() -> Unit)? = null) {
    init {
        if (callback != null) {
            callback.invoke()
        }
    }
}
s
Yup, looks like a bug to me as well. - it's in an init block, no race conditions are possible - it's a immutable
val
, cannot be changed after being set Smart cast should be able to figure that out, and it does in the 3rd example.
e
how will it if check by 'is'?
a
https://youtrack.jetbrains.com/issue/KT-4113 No smartcast for nullable lambda property (functional type) with implicit/operator
invoke
call
f
6 years ago? Wow, this seems like it would be easy to fix though.