July 2018
Intermediate to advanced
266 pages
7h 11m
English
The biggest advantage of using actors is that channels allow for more flexibility while maintaining the whole coroutine atomic. We can use this messages to expand the functionality of the actor. For example, let's make it so that we can increase or decrease the counter using the actor. First, we can add an enum with both options:
enum class Action { INCREASE, DECREASE}
And then we can update the actor and its coroutine to map these actions to operations:
var actorCounter = actor<Action>(context) { for (msg in channel) { when(msg) { Action.INCREASE -> counter++ Action.DECREASE -> counter-- } }}
And from then on, we can simply call send with the action that we want. For the sake of completion, let's ...
Read now
Unlock full access