y
03/30/2023, 4:03 AMwhen expression over the value of some type T?, it is not enough to have the cases is T and null (or is T and !is T). you still need an else case to be exhaustive. is this a known bug, or is there a reason for this?Andrew
03/30/2023, 4:05 AMinline reified method?y
03/30/2023, 4:07 AMoverride fun of an interface, the implementor is an objectAndrew
03/30/2023, 4:07 AMT is at runtime, unless you are using an inline reified functiony
03/30/2023, 4:08 AMT unifies with T? at runtime?Andrew
03/30/2023, 4:09 AMis T doesn't really do what you think it does at runtimeAndrew
03/30/2023, 4:09 AMAndrew
03/30/2023, 4:09 AMy
03/30/2023, 4:15 AMreified.
anyway, the compiler seems to reject this even here
inline fun <reified T> theValueOf(k: T?): Int =
when (k) {
is T -> 1
null -> 0
}
and here
inline fun theValueOf(k: Int?): Int =
when (k) {
is Int -> 1
null -> 0
}ephemient
03/30/2023, 5:31 AMephemient
03/30/2023, 5:33 AMsealed interface X
class Y : X
class Z : X
fun f(x: X?) = when (x) {
is Y -> 1
is Z -> 2
null -> 0
}
this works, for exampley
03/30/2023, 5:33 AMT? would be treated as sealed class (of T and null)ephemient
03/30/2023, 5:38 AMwhen cases cover the types T and Nothing?, whose union is T?, but the compiler isn't currently about to use thaty
03/30/2023, 8:41 AMis T and !is T is not enough to be exhaustive.ephemient
03/30/2023, 9:01 AMephemient
03/30/2023, 9:05 AMis T doesn't do what you want unless `<reified T : Any>`; also if your null check is first, then the subject will be smart-cast to a T & Any in following branches (such as else)