nimtiazm
04/13/2017, 9:53 AMsreich
04/13/2017, 12:30 PMnimtiazm
04/13/2017, 12:32 PMnimtiazm
04/13/2017, 12:33 PMelizarov
04/13/2017, 1:39 PMco_yield
and co_await
everywhere and the performance overhead of having to return some promise-like object. C++ cannot emulate CSP (go-style) coroutines and channels as elegantly as Kotlin does.dmitry.petrov
04/13/2017, 1:41 PMelizarov
04/13/2017, 1:42 PMkevinherron
04/13/2017, 1:42 PMdefhlt
04/13/2017, 4:57 PMnimtiazm
04/13/2017, 5:32 PMcompile("org.jetbrains.kotlinx:kotlinx-coroutines-core:$project.kotlinCoroutinesVersion")
nimtiazm
04/13/2017, 5:32 PMcbruegg
04/13/2017, 5:39 PMjkbbwr
04/13/2017, 5:40 PMjkbbwr
04/13/2017, 5:53 PMnimtiazm
04/13/2017, 5:54 PMnimtiazm
04/13/2017, 5:54 PMjkbbwr
04/13/2017, 6:00 PMcbruegg
04/13/2017, 6:05 PMsuspend
-able functions). You'll need a library like kotlinx.coroutines-core
to get implementations of stuff like `async`/`Deferred`, Sequence builders using something similar to yield
etc.cbruegg
04/13/2017, 6:05 PMjkbbwr
04/13/2017, 6:06 PMjkbbwr
04/13/2017, 6:15 PMjkbbwr
04/13/2017, 6:15 PMcbruegg
04/13/2017, 6:16 PMcontext
?jkbbwr
04/13/2017, 6:17 PM// Message types for counterActor
sealed class CounterMsg
object IncCounter : CounterMsg() // one-way message to increment counter
class GetCounter(val response: SendChannel<Int>) : CounterMsg() // a request with reply
// This function launches a new counter actor
fun counterActor() = actor<CounterMsg>(CommonPool) {
var counter = 0 // actor state
for (msg in channel) { // iterate over incoming messages
when (msg) {
is IncCounter -> counter++
is GetCounter -> msg.response.send(counter)
}
}
}
fun main(args: Array<String>) = runBlocking<Unit> {
val counter = counterActor() // create the actor
massiveRun(CommonPool) {
counter.send(IncCounter)
}
val response = Channel<Int>()
counter.send(GetCounter(response))
println("Counter = ${response.receive()}")
counter.close() // shutdown the actor
}
jkbbwr
04/13/2017, 6:17 PMchannel
come from in counterActorjkbbwr
04/13/2017, 6:18 PMcbruegg
04/13/2017, 6:18 PMactor
makes it pretty clear IMO.
public fun <E> actor( context: CoroutineContext, capacity: Int = 0, block: suspend ActorScope<E>.() -> Unit)
cbruegg
04/13/2017, 6:20 PMblock: suspend (ActorScope<E>) -> Unit
for clarity though. (I wouldn't, but maybe that'd be more obvious?)jkbbwr
04/13/2017, 6:21 PMjkbbwr
04/13/2017, 6:34 PM= x { ... }