https://kotlinlang.org logo
Title
g

groostav

12/21/2018, 7:54 PM
This smart casting thing, the more I think about it the more work it becomes:
fun `kotlin is pretty smart`(){
    val nullableSet: Set<Int>? = setOf(1, 2, 3)
    
    when(nullableSet?.size){ 
        null -> {}
        else -> { nullableSet.first() }
    }
}
I would've thought that I'd need a
nullableSet!!
in the second case. does that mean the smart-cast system was able to infer that
well the expression
nullableSet?.size
wasn't null, and
size
can never be null, so
nullableSet
must also not be null, so I can smart-cast it to
Set<Int>
if so, that kotlinc is one sharp cookie
d

Dominaezzz

12/21/2018, 8:31 PM
I think it inferred it from the assignment actually.
👎 3
s

serebit

12/21/2018, 9:10 PM
Dominic is incorrect, it did infer it from the
null
case in the
when
expression. Kotlinc is very good at smart-casting