nwh
09/21/2018, 7:23 PMcrossinline, and it still isn't fixed in coroutines 0.26.1-eap13 and kotlin 1.3.20-dev-165
import kotlinx.coroutines.*
class Handler(val func: suspend (Any) -> Unit)
// Switching crossinline to noinline fixes the error
inline fun createHandler(crossinline handler: suspend (Any) -> Unit): Handler {
return Handler({ handler.invoke(it) })
}
fun main(args: Array<String>) {
val exec = newSingleThreadContext("sample")
var handler: Handler? = null
fun send(obj: Any) = async(exec) {
handler!!.func.invoke(obj)
}
handler = createHandler({
if (it !is String)
// Removing this launch statement "fixes" the problem as well, but that's a non-solution in some cases
// Removing await also "fixes" the error
launch {
send("Handled $it").await()
}
})
// This starts to break around 10 repetitions
repeat(15) {
send(123)
}
// Delay to let events finish
runBlocking { delay(1000) }
}