kotlin coroutine actor
snippet in kotlin

kotlin coroutine actor

user2577

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)
 }
 }
}