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
Ruckus
05/19/2020, 4:50 PM
Result.Failure
is
Result<Nothing>
, whick is always satisfied.
👍 5
o
Orhan Tozan
05/19/2020, 4:55 PM
Indeed, figured it out. Works great, almost magic.