Case classes as messages

In our "hello world" example, we constructed an actor that is expected to receive a string as message. Any object can be passed as a message, provided it is immutable. It is very common to use case classes to represent messages. This is better than using strings because of the additional type safety: the compiler will catch a typo in a case class but not in a string.

Let's rewrite our EchoActor to accept instances of case classes as messages. We will make it accept two different messages: EchoMessage(message) and EchoHello, which just echoes a default message. The examples for this section and the next are in the chap09/hello_akka_case_classes directory in the sample code provided with this book (https://github.com/pbugnion/s4ds ...

Get Scala: Guide for Data Science Professionals 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.