https://kotlinlang.org logo
#announcements
Title
# announcements
b

Brian Donovan

04/23/2018, 7:39 PM
in this example, if I remove the
val callback = callback
line it no longer compiles, complaining that "reference has a nullable type". however, I would have expected that kotlin could properly smart cast
callback
to a non-optional inside the
null
check conditional. weirdly, changing
callback()
to
callback.invoke()
also makes it compile. this seems like a bug, or is
()
different from
.invoke()
on function types?
Copy code
class Thing(
    private val callback: (() -> Unit)?
) {
    fun doSomething() {
        val callback = callback
        if (callback != null) {
            callback()
        }
    }
}

Thing { println("Hi!") }.doSomething()