I noticed an interesting coroutine issue when upda...
# coroutines
r
I noticed an interesting coroutine issue when updating our app kotlin version from 1.4 to 1.6
On 1.4 there's an error but we do get the correct output
Copy code
Wrapper(value=3)
On 1.6 it just fails with a different error
There's also a slightly less functional version
map2
which works correctly on all versions
And if you take out the
delay
it obviously works because it's not suspending
Not sure if this issue is in coroutines, compiler or it's just the code is wrong but maybe someone here knows more
d
Doing some exploring. I started inlining code and this succeeds:
Copy code
inline fun <OldT, NewT> Wrapper<OldT>.map(mapFunction : (OldT) -> NewT) : Wrapper<NewT> = 
    value?.let { wrap(mapFunction(it)) } ?: Wrapper(null)
But this fails:
Copy code
inline fun <OldT, NewT> Wrapper<OldT>.map(mapFunction : (OldT) -> NewT) : Wrapper<NewT> = 
    value?.let(mapFunction.andThen(::wrap)) ?: Wrapper(null)
If I try to change
andThen
to not be an extension function, I get a compiler error:
Copy code
inline fun <OldT, NewT> Wrapper<OldT>.map(crossinline mapFunction : (OldT) -> NewT) : Wrapper<NewT> =
    value?.let(andThen(mapFunction, ::wrap)) ?: Wrapper(null)

inline fun <A, B, C> andThen(crossinline g: ((A) -> B), crossinline f: (B) -> C): (A) -> C {
    return { it: A -> f(g(it)) }
}
Suspension functions can be called only within coroutine body
I get the same error if I manually inline the map function:
Copy code
wrapped.value?.let(andThen({ oldVal ->
            delay(1000)
            oldVal.toString()
        }, ::wrap)) ?: Wrapper(null)
If I take your original code and annotate everything with
suspend
everywhere I can, it works: https://pl.kotl.in/zrgS-jyXi So it seems like something is getting mixed up in the loose way that inline allow accepting suspend funs. Note that the error is on the
T
from
Wrapper
, which indicates to me that the argument
it
to
mapperFun
in
mapOrDefault
is getting put into the wrong context somehow. It seems like a bug.
r
Thanks Dan, the other interesting thing I found but forgot to add was this bug: https://youtrack.jetbrains.com/issue/KT-44567/IncompatibleClassChangeError-caused-by-coroutines-and-Vertx
Which looks almost identical but was apparently fixed in kotlin 1.5 🤷
d
Probably worth filing a bug then
r