general question: do I have to always write a cor...
# announcements
p
general question: do I have to always write a coroutine variant of functions accepting lambdas? for example: fun doit(callback: () -> Unit) = … suspend fun doitAsync(suspend callback: () -> Unit) = … or can I somehow declare a function that accepts both suspending and non-suspending? I can imagine that ‘suspend’ adds overhead in the non-suspending case due a compiler generated state machine for supporting async. or: is it a reasonable approach to use runBlocking inside a non-suspending lambda or are there drawbacks?
e
IMO runBlocking is definitely a bad idea for something which might be called from another coroutine, you lose track of the coroutine context
p
makes sense
e
if you define
inline fun doit(callback: () -> Unit)
it should work in suspend
p
wow … inline does accept suspend_
?
e
it's not always a good idea to inline, but it is possible here
otherwise yeah, the code generation between a normal fun and a suspend fun is quite different, with the latter being translated into a state machine
p
so will runBlocking do harm besides losing track of the coroutine state? I am using 3rd party code that is not aware of suspend and I need to put my async stuff in there.
e
losing coroutine context means losing structured concurrency
p
a damn - so coroutineScope { } will not work anymore?
that explains a lot
e
the scope inside
runBlocking
will not have any relation to whatever was outside of it
p
I am using kotlin exposed and the transaction call does not support suspend
that’s the whole problem
so I have to somehow move the suspend logic outside of the db transaction I guess
e
if you have an async api available, Kotlin's job/deferred types can be adapted to futures/rx types
but if there's no async api, then this is a pain whether you're using Kotlin or not
p
yeah .. I see .. jdbc not being async is the root of all evil
lol
and we have no async driver for oracle here
thanks for your advice: I really learnt a lot