开发者

Naming case classes in Scala

开发者 https://www.devze.com 2022-12-30 03:30 出处:网络
I tend to have this redundant naming in case classes: abstract class MyTree case class MyTreeNode (...)

I tend to have this redundant naming in case classes:

abstract class MyTree
case class MyTreeNode (...)
case class MyTree开发者_StackOverflowLeaf (...)

Isn't it possible to define Node and Leaf inside of MyTree? What are best practices here?


Since class, trait and object names are package scoped, why not use the package to provide insurance against aliasing with other nodes and leafs and call them simply Node and Leaf, while leaving them outside any other scoping construct (i.e., an object)?


I wouldn't recommend putting the case classes inside their abstract superclass because nested classes are path dependent in Scala. If anything, you could 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...)

0

精彩评论

暂无评论...
验证码 换一张
取 消