-
Complete implementation.
-
Everything does what you expect.
-
No hacks, no stack manipulation, no exceptions, no magic globals.
-
Pure Scala.
import com.github.ellbur.lapper.Lapper._
import com.github.ellbur.lapper.Locked
object Test4 extends App {
{
case object Go
val keepGoing = Locked[Boolean](true)
val bob =
actor { r =>
println("Begin")
def countingLoop(n: Int): Next = {
if (n < 1000000) {
r.react {
case Go =>
countingLoop(n + 1)
}
}
else {
println("Done")
keepGoing() = false
done
}
}
countingLoop(0)
}
def spamLoop(): Unit = {
if (keepGoing()) {
bob ! Go
spamLoop()
}
}
spamLoop()
}
}
-
That little
r =>
is to avoid magic globals. It gives you access toreact
and friends. -
actor
uses trampolining to avoid growing the stack. This is why, if you are not going to callreact
again, you need to calldone
to end the trampoline. -
Every actor has its own
Thread
.