Answers for "kotlin coroutine actor"

0

kotlin coroutine actor

sealed class Msg
object IncCounter: Msg()
object PrintCounter: Msg()
class GetCounter(val resp: CompletableDeferred<Int>):Msg()
fun CoroutineScope.counterActor() = actor<Msg> {
 var counter = 0 // Actor state
 for (msg in channel) {
 when (msg) {
 is IncCounter -> counter++
 is PrintCounter -> print(counter)
 is GetCounter -> msg.resp.complete(counter)
 }
 }
}
Posted by: Guest on January-15-2021

Browse Popular Code Answers by Language