https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

marcinmoskala

01/14/2019, 8:14 AM
I am preparing Kotlin Coroutines Cheatsheet. Any suggestions? Something to change/correct?
👍 11
l

louiscad

01/14/2019, 8:24 AM
Would you publish a document/file where we can select text?
m

marcinmoskala

01/14/2019, 8:32 AM
Yes, the final one will be in PDF
e

Evgeniy Zaharov

01/14/2019, 8:52 AM
mb add more short examples?
a

antonis

01/14/2019, 8:56 AM
Hello, Some comments that popped in my head as I read this.
On the CoroutineScope interface a would put something like the following to show how it may be used class LifecycleAwareClass : CoroutineScope { private val job = Job() override val coroutineContext: CoroutineContext get() = job + TODO() //... } On the shared state. The synchronized is not the kotlin way right? Is it mentioned as an equivalent with Mutex? Regarding Actors I would used a sealed class as a base for the messages (like in the official doc https://kotlinlang.org/docs/reference/coroutines/shared-mutable-state-and-concurrency.html#actors)
s

spand

01/14/2019, 8:56 AM
Might want to be explicit that CoroutineExceptionHandler is only for uncaught exceptions
👍 1
m

marcinmoskala

01/14/2019, 9:15 AM
I removed supertype in the actor because of lack of space. Why do you say that synchronised is not the Kotlin way? It is very strongly used in stdlib and I see nothing wrong with this function.
I should probably add some ways to make a scope 🤔 Too little space to use it though. I really want to fit it all in a single page. BDW this is why I removed sealed class in the actor example - not necessary and takes extra space (especially
actor
typing makes this line too long to fit)
a

antonis

01/14/2019, 9:28 AM
Ok. I understand. it is important to fit in a single page
I thought that Mutex is the way to go for Kotlin in use cases that you would use syncronized https://kotlinlang.org/docs/reference/coroutines/shared-mutable-state-and-concurrency.html#mutual-exclusion
👍 2
I did not have many cases to use this in the real world yet. But I thought that is what the doc is suggesting
s

Slava Glushenkov

01/14/2019, 9:32 AM
Sequance != Sequence
👏 1
l

louiscad

01/14/2019, 10:57 AM
@marcinmoskala Of course it will be in pdf, but could we have it in pdf now so we can select text now to give you feedback?
@marcinmoskala You could add a password to the WIP pdf file so it doesn't get in the wild so easily
m

marcinmoskala

01/14/2019, 1:00 PM
On the soon it will go out anyway 😉 I will send it today
Here is the file after corrections. Though selection is still not possible because we are sending it to another person to generate selectable version (Adobe Illustrator is needed for that)
👍 5
g

groostav

01/14/2019, 6:16 PM
maybe
Channels
can get into
failed
vs
closed
symatics? Thats one of the single biggest things that's tripped me up. I'm still not really sure of
chan.consumeEach
vs
for(it in chan)
with respect to closed and failed channels
👍 1
Copy code
val producer = produce {
            for(i in 1 .. 5){ send(i * i) }
        }

        println(producer.receive()) //1
        println(producer.receive()) //4

        for(element in producer){
            println(element) //9, 16, 25
        }
? Also consider that
consumeEach
has scary warnings on it about being changed in future is tagged with
ObsoleteCoroutinesApi
also
Dispatchers.Unconfined
does not change thread implies something semantic when I really think its only safe to use this for performance/debugging. Maybe say "Use first available dispatcher for performance"?
👍 1
a

Allan Wang

01/14/2019, 8:43 PM
Does
Dispatchers.Default
really use "a different thread if possible"? It just submits the request to a "default" thread pool doesn't it?
m

marcinmoskala

01/16/2019, 11:05 AM
@Allan Wang Well, on JVM. On JS there are no separate threads.
l

louiscad

01/16/2019, 8:23 PM
Since the cheat sheet does cover a library that can change and has experimental and future obsolete APIs, I'd specify kotlinx.coroutines version at the top, and also specify the last updated date of the sheet (or something like 2019.1, JetBrains like). You know, time never stops.
☝️ 1
m

marcinmoskala

01/17/2019, 10:03 PM
s

Saiedmomen

01/18/2019, 7:29 PM
Thats really awesome Might I suggest that you use the table format? It think it would be easier to comprehend and more complete that way.
For example in the builders section I can't understand right now if
withContext
is blocking
12 Views