Hey folks. Just noticed this issue <https://youtra...
# stdlib
m
Hey folks. Just noticed this issue https://youtrack.jetbrains.com/issue/KT-44867 where
Result.getOrThrow
throw a ClassCastException even if the result is successful. Is there any workaround while the fix is being released. My app depends on it to work properly
👀 1
e
use your own Result type?
m
Sure. Ideally I’d like to avoid that as it would mean to rewrite a significant aspect of the app.
I actually found workaround that works. It’s not great though but it does the job
Copy code
//Necessary because of <https://youtrack.jetbrains.com/issue/KT-44867>
private fun <T> Result<T>.unwrap(): T {
    return fold(
        onSuccess = {
            when (it) {
                is Result<*> -> it.getOrThrow() as T
                else -> it
            }
        },
        onFailure = { t -> throw t }
    )
}
i
The fix of that issue was released in Kotlin 1.5. Which version of Kotlin do you use?
m
1.5.10
c
fix in builds says 1.5.20 and 1.5.30 only
i
@christophsturm The fixing commit was backported to 1.5.0 as well.
@mboudraa I've tried the code example from the issue and it doesn't cause
ClassCastException
either in Kotlin 1.5.0 or 1.5.10 anymore. There are some other known problems with
kotlin.Result
leading to
ClassCastException
, though. Could you provide the code that reproduces this problem in your case?