Reputation: 47649
In the Scala actor examples I have seen where a parameterless message is sent to an actor (such as this), case class
es (or case object
s) have been created and then used as messages. Symbols work just as well and look a bit neater and, after reading a book on Erlang, seem more natural. I assume that symbol equality would work for remote actors.
For messages with parameters case classes would be the obvious choice, so perhaps consistency between message types is one issue?
Are there any reasons to go for either approach?
Upvotes: 3
Views: 444
Reputation: 11596
The short answer is compile-time checking.
Even thought symbols can be used as messages and they are even more succinct than case objects (no need to define them), compiler can not detect the misspelled symbols and you will have a hard time figuring out why actors aren't receiving specific messages when they are supposed to.
If case classes and/or objects are used as messages, compiler will tell you if you are trying to send and/or receive non-existent messages before the program is even executed.
Upvotes: 6
Reputation: 134310
I don't think that Symbol
s are a substitute for using case
classes. In fact, I'm not entirely sure what use Symbol
is at all, given it lacks the power of symbols in other languages (e.g. Ruby, Smalltalk) - it's just an interned String.
For example, in the standard auction example, it's difficult to see how you would represent the complexity of a bid / offer using symbols alone.
As for case
objects, I also believe that these are preferable to symbols. For example, they can be instances of trait
s etc, and hence supply functionality.
Upvotes: 3