Reputation: 17785
IS there a suggested best practise for where to define your case classes in Scala? For example, suppose you have a few actors sending messages to each other where should the case classes be defined?
In a shared package? In one of the actors - if so which one?
Or in a package object, for example:
package object mypackage {
case object Ping
case object Pong
case object Stop
}
if the actors are in the same package?
Just trying to figure out best practise here.
Upvotes: 3
Views: 186
Reputation: 3608
I have not found a best practise for this issue, but I can tell you how I do it. For me it depends on the complexity of the messages. If I have highly branched messages with inheritance, I usually put them into an own package called "message". If I have only some messages used by many Actors in the same package, I define the messages in a package object. But if there are messages, that are only used by a single actor (for example if I want to emulate a private recursive function), I define these messages in the Actor using them. Sometimes a mix of all of this might be the best way to go on this.
Upvotes: 2
Reputation: 1900
I Usually put the message case classes in with the actor's themselves, the downside is that you can't share message classes between actors in that way, but in practice that has never been a problem for me and it's a bit more verbose when calling them eq:
MyActor ! MyActor.MyMessage
Upvotes: 2