Name class classes in Scala

I tend to have this redundant naming in case classes:

abstract class MyTree
case class MyTreeNode (...)
case class MyTreeLeaf (...)

      

Is it not possible to define Node and leaf inside MyTree? What are the best practices here?

+2


a source to share


2 answers


Since the names of classes, traits, and objects are limited to packages, why not use a package to provide insurance against anti-aliasing with other nodes and leaves, and just name them Node

and Leaf

leave them out of any other scope (i.e. object)?



+4


a source


I would not recommend putting case classes in their abstract superclass, because nested classes are path dependent in Scala. If anything, you can put them inside a companion object.

abstract class MyTree
object MyTree {
  case class Node (...) extends MyTree
  case class Leaf (...) extends MyTree
}

      



(note: I haven't tested this ...)

+2


a source







All Articles