Not sure if this is the best channel to ask, but d...
# compiler
w
Not sure if this is the best channel to ask, but does anyone know some compiler plugin which can turn a class with suspend (or non-suspend) functions and also a whole module or individual functions into non-suspend (or suspend) versions, so one doesn’t have to copy-paste lots of code and keep the two versions in sync by hand? The function coloring problem keeps hitting us every now and then and
inline
can’t always be used to work around it.
e
What function coloring problem and how does inline work around it?? Can you post a code example?
w
With the “function coloring problem” I was referring to the post by Bob Nystrom which received some attention: https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ Basically, the core issue is the infectiousness of
suspend
(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:
Copy code
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.
e
This means your code gets split into two incompatible worlds
Your 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 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
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 computations.
With
inline
you can partially work around this:
This is a complete misconstruing of what
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 turn
foo
into a
suspend
function only when needed while in all other cases it’s a synchronous non-suspend function.
No. The compiler does no such thing. See screen shot, if
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.
w
Your 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.
That’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.
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 computations
I 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 what
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.
I know what
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, if
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.
The 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
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.