Robert Williams
05/15/2022, 11:04 AMRobert Williams
05/15/2022, 11:04 AMRobert Williams
05/15/2022, 11:05 AMWrapper(value=3)
Robert Williams
05/15/2022, 11:05 AMRobert Williams
05/15/2022, 11:05 AMmap2
which works correctly on all versionsRobert Williams
05/15/2022, 11:06 AMdelay
it obviously works because it's not suspendingRobert Williams
05/15/2022, 11:08 AMDan Fingal-Surma
05/16/2022, 7:19 AMinline fun <OldT, NewT> Wrapper<OldT>.map(mapFunction : (OldT) -> NewT) : Wrapper<NewT> =
value?.let { wrap(mapFunction(it)) } ?: Wrapper(null)
But this fails:
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:
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:
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.Robert Williams
05/16/2022, 9:22 AMRobert Williams
05/16/2022, 9:23 AMDan Fingal-Surma
05/17/2022, 12:57 AMRobert Williams
05/17/2022, 8:55 AM