title: Basic Scala Actors: examples from books not "acting" at all
body: <p>I'm very new to Scala, and too rusty in Java to consider myself anything else than a complete newbie. So I'm taking easy steps to learn it.</p>
  
  <p>When looking at actors, I tried a few things, but faced many NoClassDefFound errors. Ultimately, I decided to take a book example and build on top of it instead of debugging my first attempt. Surprise: the book examples don't work as expected!</p>
  
  <p>Here's the example from O'Reilly's Programming Scala:</p>
  
  <pre><code>import scala.actors.Actor
  
  class Redford extends Actor {
    def act() {
      println("A lot of what acting is, is paying attention.")
    }
  }
  
  val robert = new Redford
  robert.start
  </code></pre>
  
  <p>It's supposed, when executed, to print the Redford quote.  However when I launch it, nothing happens, I get back to command line:</p>
  
  <pre><code>D:\prog\scala-2.8.1.final\pierric&gt;scala testactors.scala
  
  D:\prog\scala-2.8.1.final\pierric&gt;
  </code></pre>
  
  <p>Another example comes from Seven Programming Languages in Seven Weeks. It's like this (I only changed the strings out of laziness):</p>
  
  <pre><code>import scala.actors._
  import scala.actors.Actor._
  
  case object Poke;
  case object Feed;
  
  class Kid() extends Actor {
      def act() {
          loop {
              react {
                  case Poke =&gt; {
                      println("Ow")
                      println("Quit it")
                  }
                  case Feed =&gt; {
                      println("gurgle")
                      println("burp")
                  }
              }
          }
      }
  }
  
  var bart = new Kid().start
  var lisa = new Kid().start
  println("starting")
  bart ! Poke
  lisa ! Poke
  bart ! Feed
  lisa ! Feed
  </code></pre>
  
  <p>This time it's supposed to return a randomly ordered sequence of "ow quit it" and "gurgle burp". However, when I run it:</p>
  
  <pre><code>D:\prog\scala-2.8.1.final\pierric&gt;scala testkids.scala
  starting
  
  D:\prog\scala-2.8.1.final\pierric&gt;
  </code></pre>
  
  <p>Now, another amusing thing. If I add a simple println line at the beginning of my act method:</p>
  
  <pre><code>class Kid() extends Actor {
      def act() {
          println("Kid initializing")
          loop {
              react {
                  ...
  </code></pre>
  
  <p>Then I get most of the times:</p>
  
  <pre><code>D:\prog\scala-2.8.1.final\pierric&gt;scala testkids.scala
  starting
  Kid initializing
  Kid initializing
  
  D:\prog\scala-2.8.1.final\pierric&gt;
  </code></pre>
  
  <p>But sometimes also:</p>
  
  <pre><code>starting
  Kid initializing
  Kid initializing
  scala.actors.Actor$$anon$1@5a9de6: caught java.lang.NoClassDefFoundError: Main$$anon$1$Fee
      java.lang.NoClassDefFoundError: Main$$anon$1$Feed$
              at Main$$anon$1.Main$$anon$$Feed(testkids.scala:5)
              at Main$$anon$1$$anonfun$1.apply$mcV$sp(testkids.scala:31)
              at scala.actors.Actor$$anon$1.act(Actor.scala:135)
              at scala.actors.Reactor$$anonfun$dostart$1.apply(Reactor.scala:222)
              at scala.actors.Reactor$$anonfun$dostart$1.apply(Reactor.scala:222)
              at scala.actors.ReactorTask.run(ReactorTask.scala:36)
              at scala.concurrent.forkjoin.ForkJoinPool$AdaptedRunnable.exec(ForkJoinPool.java:6
              at scala.concurrent.forkjoin.ForkJoinTask.quietlyExec(ForkJoinTask.java:422)
              at scala.concurrent.forkjoin.ForkJoinWorkerThread.mainLoop(ForkJoinWorkerThread.ja
              at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:32
      Caused by: java.lang.ClassNotFoundException: Main$$anon$1$Feed$
              at java.net.URLClassLoader$1.run(Unknown Source)
  </code></pre>
  
  <p>So here I am, clueless... as those are examples "out of the book", actually out of 2 books! and don't seem to work. I've tried on 2 different machines, which in all likeliness have different JVM's. In both cases I've run scala 2.8.1.final. One machine runs Windows XP 32-bit, the other one Windows 7 64-bit. I didn't find anything related to this kind of issue by googling...</p>
  
  <p>Thanks in advance to anyone who can shed a light on this!</p>
  
  <p>Pierric.</p>