Sending and Receiving Messages

We can send just about any type of message to an actor—String, Integer, Long, Double, List, Map, tuples, Scala case classes…all of which are immutable. I have a special liking for tuples, not because it’s amusing when we mispronounce them as “two-ples” but because they’re lightweight, immutable, and one of the easiest instances to create. For example, to create a tuple of two numbers in Scala, we simply write (number1, number2). Scala’s case classes are ideal to serve as message types—they’re immutable, work well with pattern matching, and are quite easy to make copy of. In Java we could pass an unmodifiable Collection as messages to send more than one object in a message. When we pass a message to an actor, by ...

Get Programming Concurrency on the JVM now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.