jwass
10/28/2021, 10:18 AMsealed class Resolvable<T> {
/** There's a value in either case.
*/
fun unwrap(): T = when (this) {
is Resolved -> this.value
is Unresolved -> this.value
}
fun unwrap2(): T =
if (this is Resolved<T>) {
this.value
} else if (this is Unresolved<T>) {
this.value
}
}
data class Resolved<T>(val value: T, val pk: Long) : Resolvable<T>()
data class Unresolved<T>(val value: T) : Resolvable<T>()
My compiler is happy with the unwrap()
when I write it with a when
. But with an if
there's a problem with the else
branch, and it thinks that the return type is Unit
not T
. Any ideas why the when
version is OK but the if
version is not?