Waldemar Kornewald
05/16/2023, 12:41 PMinline
can’t always be used to work around it.efemoney
05/16/2023, 3:38 PMWaldemar Kornewald
05/17/2023, 6:10 AMsuspend
(or async
), i.e. it infects the caller which now also has to use suspend
or at least launch
. This means your code gets split into two incompatible worlds and you need to duplicate code under different names to make it usable for both worlds. Infectiousness of any kind is always bad (there are also architectural decisions which lead to infectious patterns).
For instance, I’ve written a parser library, but it’s not using suspend
because it needs to be usable without coroutines. Now I need the same API for streaming parsing and the most natural solution would be to use suspend
. I don’t want to duplicate all that code because this risks introducing too many bugs/inconsistencies during maintenance. It’s a mostly mechanical transformation that the compiler could do for me. Or at least a compiler plugin could help.
With inline
you can partially work around this:
inline fun foo(block: () -> Unit) { ... }
Although you didn’t declare foo
and block
as suspend
the inline
makes them compatible. So you can call foo { synchronousCall() }
but also foo { suspendCall() }
. The compiler will do the right thing and turn foo
into a suspend
function only when needed while in all other cases it’s a synchronous non-suspend function. Ideally, the compiler would be able to do this with every function and we could get rid of suspend
completely.efemoney
05/17/2023, 10:33 AMThis means your code gets split into two incompatible worldsYour code is already in two incompatible worlds.
suspend fun
means this function can “suspend” execution of its caller while it does its work. It comes with many concepts but that basic one is the building block for ALL coroutines.
normal fun
cannot and will not have the same behavior. You cannot model what suspend fun
model using normal functions.
For instance, I’ve written a parser library, but it’s not usingWhich brings me to this part. You’ve written a blocking parser library. The natural solution is actually not to duplicate the API & implement streaming in coroutines but to implement streaming using blocking code just like you have the base API. You cannot have non-blocking (suspendable) code with an API that is blocking. Normal functions just cannot model suspendable computations.because it needs to be usable without coroutines. Now I need the same API for streaming parsing and the most natural solution would be to usesuspend
suspend
WithThis is a complete misconstruing of whatyou can partially work around this:inline
inline
does. It’s simple and should not be taken to mean anything other than what it does which is inline its function body at the call site! Basically the body of the function runs within the context of the caller. If that is a suspendable context then the body of the inlined lambda can call suspend functions. If the context is not suspendable then you cannot call suspend functions. It is not a way to workaround anything neither does it make two incompatible things compatible.
The compiler will do the right thing and turnNo. The compiler does no such thing. See screen shot, ifinto afoo
function only when needed while in all other cases it’s a synchronous non-suspend function.suspend
foo
“_*became suspendable*_” via compiler magic, it will have a suspend icon in the gutter beside line number 6 but it doesnt because it is neither a normal function nor a suspend function. Its an inline function with defined semantics.Waldemar Kornewald
05/17/2023, 11:10 AMYour code is already in two incompatible worlds.
means this function can “suspend” execution of its caller while it does its work. It comes with many concepts but that basic one is the building block for ALL coroutines.suspend fun
normalThat’s not true. This is just a limitation of the language. The compiler could solve this automatically, but it would be more effort and maybe slower to analyze. At the very least you can solve it like in Java’s Project Loom or Go. There you don’t have this two worlds problem. The two worlds problem is not a fundamental limitation of asynchronous programming. It’s only a limitation of the Kotlin approach.cannot and will not have the same behavior. You cannot model whatfun
model using normal functions.suspend fun
Which brings me to this part. You’ve written a blocking parser library. The natural solution is actually not to duplicate the API & implement streaming in coroutines but to implement streaming using blocking code just like you have the base API. You cannot have non-blocking (suspendable) code with an API that is blocking. Normal functions just cannot model suspendable computationsI want to have a very natural API for the parser which is on par with PEGs, but is just normal Kotlin code. The blocking version already achieves this goal. By putting
suspend
everywhere I can keep the natural API and make it non-blocking. What’s your alternative suggestion? I don’t want to have RxJava-style operator chains like the other parser libraries because then the code becomes ugly and limited.
This is a complete misconstruing of whatI know whatdoes. It’s simple and should not be taken to mean anything other than what it does which is inline its function body at the call site! Basically the body of the function runs within the context of the caller. If that is a suspendable context then the body of the inlined lambda can call suspend functions. If the context is not suspendable then you cannot call suspend functions. It is not a way to workaround anything neither does it make two incompatible things compatible.inline
inline
really means. However, even in the stdlib Kotlin uses inline
in a lot of places which look a lot like they were chosen on purpose to be compatible with both suspend and non-suspend. And it’s the only real workaround for making functions work in both worlds. That’s why I mentioned inline.
No. The compiler does no such thing. See screen shot, ifThe compiler simply inlines the block. The function foo doesn’t really exist anymore after inlining, so from a pedantic point of view foo doesn’t become suspendable because there is no foo at all. However, the point is that you can use“_*became suspendable*_” via compiler magic, it will have a suspend icon in the gutter beside line number 6 but it doesnt because it is neither a normal function nor a suspend function. Its an inline function with defined semantics.foo
foo
with sus
even though foo
is not marked as suspend
. So from a practical point of view, foo is really compatible with two worlds only because it uses inline
. This is exactly what I said right from the start.