I've got another question related to the above exa...
# codereview
j
I've got another question related to the above example. I have a sealed class, and both implementations contain the 'same' field. So I want a function on the abstract type to retrieve it.
Copy code
sealed 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?