Question

In the Scala actor examples I have seen where a parameterless message is sent to an actor (such as this), case classes (or case objects) 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?

Was it helpful?

Solution

I don't think that Symbols 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 traits etc, and hence supply functionality.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top