A good place to define your classes?

Is there a suggested best practice for defining case classes in Scala? For example, suppose you have multiple actors sending messages to each other, where should the case classes be defined?

In the general package? In one of the actors - if so?

Or in a package object like:

package object mypackage {
  case object Ping
  case object Pong
  case object Stop
}

      

if the participants are in the same package?

Just trying to find the best practice here.

+3


source to share


2 answers


I used to put the message classes with the actor itself, the downside is that you cannot share the message classes between the participants in this way, but in practice this has never been a problem for me, and it's a little more verbose when calling eq:



MyActor ! MyActor.MyMessage

      

+2


source


I haven't found any best practice for this problem, 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 in my own package called "message". If I only have some messages used by many contributors in one package, I define the messages in the package object. But if there are messages that are only used by one Actor (for example, if I want to emulate a private recursive function), I define those messages in the Actor using them. Sometimes a combination of all of these can be the best way to do this.



+2


source







All Articles