package coroutine import scala.util.continuations._ abstract class Coroutine { private var started = false private var _session: Unit => Unit = null def act (): Unit @suspendable final def end (): Unit @suspendable = { _session = null } final def start () { reset { started = true act () end () } // reset } // start final def resume () { _session () } final def yyield (coroutine: Coroutine): Unit @suspendable = { shift { remaining: (Unit => Unit) => { _session = remaining if (coroutine.started == false) { coroutine.start() } else { coroutine.resume() } // if } } // shift } // yyield } // Coroutine object CoroutineTest extends App { class Cor1 extends Coroutine { def act (): Unit @suspendable = { println ("Cor1: phase 1 ") yyield (cor2) println ("Cor1: phase 2") yyield (cor2) } // act } // Cor1 class Cor2 extends Coroutine { def act (): Unit @suspendable = { println ("Cor2: phase 1") yyield (cor1) println ("Cor2: phase 2") } // act } // Cor2 val cor1 = new Cor1 () val cor2 = new Cor2 () println ("start coroutines") cor1.start () } // CoroutineTest