Hello, It looks like Kotlin 1.4 is bringing new st...
# announcements
e
Hello, It looks like Kotlin 1.4 is bringing new stuff! Any reason why this breaking change? Code
Copy code
fun <T: Any> x(block: () -> T?) {
    val res = block()
    if (res != null) {
        println(res)
    } else {
        println("returned null")
    }
}

fun test() {
    x<Unit> {
        null
    }
}
Kotlin (<1.4) It prints "retuned null Kotlin (>= 1.4) It prints kotlin.Unit The weird part is that
x { null }
works fine. The compiler is able to infer the type to
Unit?
. But I can't write
x<Unit?> { null }
as the type is bound to
Any
which is something we need on Kotlin-Native (unless this has changed too)
t
I can't tell you why 1.4. lets the lambda return
Unit
instead of the last statement which is
null
, but what you call "the weird part" isn't really weird at all. The type
Unti?
isn't inferred, it's specified. The return type of `x`'s lambda parameter is
T?
, so because your call explicitly specifies
T
as
Unit
, the lambda return type is
Unit?
i
This looks much like https://youtrack.jetbrains.com/issue/KT-41005 and this issue is fixed in 1.4.10. Could you upgrade to that new version and check whether it happens then?
e
@ilya.gorbunov Thanks for your help. This has been fixed in 1.4.10