Why does the following code compile? ```fun <T,...
# announcements
o
Why does the following code compile?
Copy code
fun <T, R> Result<T>.letIfSuccess(
    block: (data: T) -> Result<R>
): Result<R> = when (this) {
    is Result.Failure -> this
    is Result.Success -> block(this.data)
}
letIfSuccess returns
Result<R>
, but returns
Result<T>
when first branch of when statement is met. I would expect it to give it a compile error, as
Result<T> !is Result<R>
.
r
Result.Failure
is
Result<Nothing>
, whick is always satisfied.
👍 5
o
Indeed, figured it out. Works great, almost magic.