Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin coroutines: possible without standard library?

My question is rather theoretical. I am quite new to kotlin (only passed the tutorial, didn't write any real code).

Reading through the language reference I find myself confused about the fact that "suspend" is a keyword, yet I can't find anything like "launch" in the list of keywords. That makes me think that there is some asymmetry - the "suspend" is a compiler feature, yet "launch" is a library function. Is my understanding correct? If so - wouldn't it have been better to implement both as library features or both as compiler features?

I always thought that you can always write your own standard library using the available language features, but I still can't see if this really applies to this case.

TL;DR: Can I start a coroutine using pure kotlin, without importing any libraries whatsoever (however ugly that would be)?

like image 764
Alexey Nezhdanov Avatar asked Oct 04 '19 13:10

Alexey Nezhdanov


2 Answers

The suspend marker adds a hidden continuation parameter to the function signature and completely changes the implementation bytecode. Suspension points don't boil down to helper function calls, they turn your linear program code into a state machine, the state being kept in the continuation object. The resulting bytecode isn't even representable as Java program code.

As opposed to that, launch is just regular library code that builds upon the suspend/resume primitive.

like image 182
Marko Topolnik Avatar answered Sep 21 '22 23:09

Marko Topolnik


Can I start a coroutine using pure kotlin, without importing any libraries whatsoever (however ugly that would be)?

No. All coroutine generators are inside kotlinx.coroutines library, so you'll need at least that. Now, very theoretically, you could reimplement this functionality yourself. But probably you shouldn't.

How this can be done is a bit too long for a StackOverflow answer, but try invoking method of this Kotlin class from Java:

class AsyncWorks {
    suspend fun print() {
        println("Hello")
    }
}

You'll see that although Kotlin method has no arguments, in Java it requires Continuation<? super Unit>. This is what suspend keyword does. It adds Continuation<T> as the last argument of our function.

wouldn't it have been better to implement both as library features or both as compiler features?

Ideally, you'd want everything to be a "library feature", since it's easier to evolve. Removing a keyword from a language is very hard. In theory, having suspend as a keyword could be avoided. Quasar, being a framework, uses annotations instead. Go programming language, on the other hand, assumes all functions are suspendable. All those approaches have their advantages and disadvantages.
Kotlin decided to be pragmatic, and add suspend keyword, leaving the decision on the developers. If you're interested in the topic, I highly recommend this talk by Roman Elizarov, author of Kotlin coroutines, that explains their decissions: https://www.youtube.com/watch?v=Mj5P47F6nJg

like image 29
Alexey Soshin Avatar answered Sep 20 '22 23:09

Alexey Soshin