In my Kotlin app I have nullable variable like this
private var myCallback : (() -> Unit)? = null
Is it possible to use null safety operator ?
to call it? This gives me a compilation error.
myCallback?()
I found only this ugly way for a call if it is not null
if(myCallback != null)
myCallback!!()
You can call it as follows:
myCallback?.invoke()
The ()
syntax on variables of function types is simply syntax sugar for the invoke()
operator, which can be called using the regular safe call syntax if you expand it.