Using kotlin.Result causes runtime errors (up to 1.5.30-RC)

I’ve been experiencing runtime errors while using the new kotlin 1.5 Result value class.

I’ve migrated a rather large project to 1.5, and started to use the newly stabilized kotlin.Result in my code. However, 1.5.0, 1.5.10, 1.5.20 and 1.5.21 have all caused weird runtime errors, ranging from ClassCastException to NoSuchMethodError. Even updating to 1.5.30-RC doesn’t solve this, although the errors have changed.

I’ve been having to use a substitute class to fix my issues:

sealed class Outcome2<out T> { abstract val value: T abstract val error: Throwable? private data class Success<out T>( override val value: T ): Outcome2<T>() { override val error: Nothing? get() = null } private data class Failure( override val error: Throwable ): Outcome2<Nothing>() { override val value: Nothing get() = throw error } companion object { fun <T> success(value: T): Outcome<T> = Success(value) fun failure(exception: Throwable): Outcome<Nothing> = Failure(exception) } }

The above sealed class (with appropriate extension functions) works just like a Result.

Apparently, the kotlin compiler doesn’t transpile value classes properly, especially when they’re used in suspend functions. The Jetbrains team has been chasing down these bugs, but apparently, my issues remain, months after 1.5.0 was released.

submitted by /u/togrias
[link] [comments]