Saturday, October 8, 2011

When to use case classes ?

It is very important that all messages that will be sent around in the system are immutable. The Actor model relies on the simple fact that no state is shared between Actors and the only way to guarantee that is to make sure we don’t pass mutable state around as part of the messages.

In Scala we have something called case classes. These make excellent messages since they are both immutable and great to pattern match on.


class Person {  
   def tell { 
   println( "here's a little story ..." ) 
   } 
}  
val singingPerson = new Person with Singer 
singingperson.sing
def cast( p: Person ) {     
   p match {       
   case s: Singer => s.sing       
   case _ => p.tell    
   } 
}

No comments: