CLOVIS
07/16/2025, 2:16 PMinterface Foo
class A(val a: String?) : Foo
class B(val b: String) : Foo
val f = listOf(A("1"), A("2"), B("3"))
val d = f.mapNotNull {
when (it) {
is A -> it.a
is B -> it.b
}
}
Without looking it up, what do you think is the type of d
?Sergey Dmitriev
07/16/2025, 2:17 PMList<String?>
?CLOVIS
07/16/2025, 2:18 PMList<Unit>
🤡CLOVIS
07/16/2025, 2:18 PMList<String>
if you make Foo
sealed
Sergey Dmitriev
07/16/2025, 2:19 PMwhen
exhaustive?CLOVIS
07/16/2025, 2:19 PMCLOVIS
07/16/2025, 2:19 PMelse
, it's List<String>
tooCLOVIS
07/16/2025, 2:19 PMSergey Dmitriev
07/16/2025, 2:19 PMCLOVIS
07/16/2025, 2:20 PMJonathan
07/16/2025, 2:21 PMreturn
? Without it the compiler is maybe auto inserting Unit
?CLOVIS
07/16/2025, 2:22 PMwhen
is not exhaustive it's an instruction and not an expression, and a lambda that ends with an instruction returns Unit
Jonathan
07/16/2025, 2:22 PMa lambda that ends with an instruction returnsTILUnit
Youssef Shoaib [MOD]
07/16/2025, 2:28 PM