From afa0379466405a1fa7f499b714e2e9ac4351efdc Mon Sep 17 00:00:00 2001 From: Philipp Haller Date: Fri, 21 Sep 2007 18:22:41 +0000 Subject: Made fringe example more type safe. --- docs/examples/actors/fringe.scala | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'docs') 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 => -- cgit v1.2.3