diff options
author | Philipp Haller <hallerp@gmail.com> | 2007-09-21 18:22:41 +0000 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2007-09-21 18:22:41 +0000 |
commit | afa0379466405a1fa7f499b714e2e9ac4351efdc (patch) | |
tree | 9b5ec1e2f893e8c58703ea82183368369ca0d7f6 /docs | |
parent | fffae187752e9641b2d01265b77fe5f5d173987e (diff) | |
download | scala-afa0379466405a1fa7f499b714e2e9ac4351efdc.tar.gz scala-afa0379466405a1fa7f499b714e2e9ac4351efdc.tar.bz2 scala-afa0379466405a1fa7f499b714e2e9ac4351efdc.zip |
Made fringe example more type safe.
Diffstat (limited to 'docs')
-rw-r--r-- | docs/examples/actors/fringe.scala | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/docs/examples/actors/fringe.scala b/docs/examples/actors/fringe.scala index 96edd396e9..0d00dc857c 100644 --- a/docs/examples/actors/fringe.scala +++ b/docs/examples/actors/fringe.scala @@ -5,7 +5,7 @@ import scala.actors.{Channel, OutputChannel} /** @author Philipp Haller - @version 1.0, 07/09/2007 + @version 1.1, 09/21/2007 */ object fringe extends Application { @@ -13,19 +13,24 @@ object fringe extends Application { case class Node(left: Tree, right: Tree) extends Tree case class Leaf(v: Int) extends Tree + case class CompareFringe(t1: Tree, t2: Tree) + case class ComputeFringe(t1: Tree, atoms: OutputChannel[Option[Leaf]]) + case class Equal(atom1: Option[Leaf], atom2: Option[Leaf]) + case class Extract(tree: Tree) + val comparator = actor { val extractor1 = actor(extractorBehavior()) val extractor2 = actor(extractorBehavior()) - val ch1 = new Channel[Any] - val ch2 = new Channel[Any] + val ch1 = new Channel[Option[Leaf]] + val ch2 = new Channel[Option[Leaf]] loop { react { - case ('Fringe, tree1, tree2) => - extractor1.send(('Fringe, tree1), ch1) - extractor2.send(('Fringe, tree2), ch2) - self ! Triple('Equal, ch1.?, ch2.?) + case CompareFringe(tree1, tree2) => + extractor1 ! ComputeFringe(tree1, ch1) + extractor2 ! ComputeFringe(tree2, ch2) + self ! Equal(ch1.?, ch2.?) - case ('Equal, atom1, atom2) => + case Equal(atom1, atom2) => println("comparing "+atom1+" and "+atom2) if (atom1 == atom2) atom1 match { case None => @@ -42,14 +47,14 @@ object fringe extends Application { } val extractorBehavior = () => { - var output: OutputChannel[Any] = null + var output: OutputChannel[Option[Leaf]] = null loop { react { - case ('Fringe, tree) => - output = sender - self ! ('Extract, tree) + case ComputeFringe(tree, leafSink) => + output = leafSink + self ! Extract(tree) - case ('Extract, tree) => tree match { + case Extract(tree) => tree match { case atom @ Leaf(_) => println("sending "+Some(atom)) output ! Some(atom) @@ -61,10 +66,10 @@ object fringe extends Application { val cont = actor { react { case 'Continue => - outer.send(('Extract, right), outerCont) + outer.send(Extract(right), outerCont) } } - self.send(('Extract, left), cont) + self.send(Extract(left), cont) } case 'Continue => |