Jess Brent
04/07/2020, 7:44 PMinline fun <reified T> withResult(
method: Result<T>,
onFailure: (errors: List<Throwable>) -> Unit
): T {
@Suppress("IMPLICIT_CAST_TO_ANY")
val methodResult = when (method) {
is Result.Success -> { method.t }
is Result.Failure -> { onFailure.invoke(method.errors) }
}
return methodResult as T
}
usage:
val r = withResult(method) { return Result.Failure(it) }
i can get this function to work on jvm, however it does not work on js. it results in a ClassCastException
the transpiled js appears to show that instead of invoking onFailure
it is only assigned to methodResult
.
also, this is a custom sealed class Result
not kotlin.Result
i’m hoping i’m missing something obvious here. 😅invoke()
and it works