Hey guys. I was playing around with using `Result`...
# coroutines
u
Hey guys. I was playing around with using
Result
as a return type and ran into a weird issue. Does anybody know if this is already known? I couldn't find anything in the issue tracker.
Copy code
suspend fun foo() = suspendCancellableCoroutine<Unit> {
        thread {
            it.resumeWithException(RuntimeException("foo"))
        }
    }

    suspend fun fooWithResult(): Result<Unit> = runCatching { foo() }

    @Test
    fun resultTest() = runBlocking {
        // when fooWithResult is not inlined the exception is not caught by "runCatching"
        val result = fooWithResult() 
        Assertions.assertTrue { result.isFailure }
        delay(5000)
        Unit
    }
The test passes if I mark
fooWithResult
as inline. Without it the exception is not caught by
runCatching
a
Coroutines returning kotlin.Result is an issue, at least in 1.3.x, because it’s used internally. I came across this, and there was some combination of either compiler or intellij that flagged it as a warning
u
Yes, you have to enable the usage via
Copy code
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs += ["-Xinline-classes", "-Xallow-result-return-type"]
    }
}
Which stops the IDE from complaining and it does work in general. But might be a little rough around the edges.
Will create an issue on Youtrack later if nobody objects.
i
"-Xallow-result-return-type"
Do not do this. The error is here to prevent exactly this behavior until the proper fix is implemented.
u
Interesting. I was under the impression that these restrictions have been lifted with 1.4. Good to know. Thanks!