I am preparing Kotlin Coroutines Cheatsheet. Any s...
# coroutines
m
I am preparing Kotlin Coroutines Cheatsheet. Any suggestions? Something to change/correct?
πŸ‘ 11
l
Would you publish a document/file where we can select text?
m
Yes, the final one will be in PDF
e
mb add more short examples?
a
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
Might want to be explicit that CoroutineExceptionHandler is only for uncaught exceptions
πŸ‘ 1
m
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
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
Sequance != Sequence
πŸ‘ 1
l
@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
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
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
Does
Dispatchers.Default
really use "a different thread if possible"? It just submits the request to a "default" thread pool doesn't it?
m
@Allan Wang Well, on JVM. On JS there are no separate threads.
l
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
s
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