Scala 中的命名案例类
我倾向于在案例类中使用这种冗余命名:
abstract class MyTree
case class MyTreeNode (...)
case class MyTreeLeaf (...)
难道不能在 MyTree 的内部定义节点和叶子吗? 这里的最佳实践是什么?
I tend to have this redundant naming in case classes:
abstract class MyTree
case class MyTreeNode (...)
case class MyTreeLeaf (...)
Isn't it possible to define Node and Leaf inside of MyTree?
What are best practices here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于类、特征和对象名称是包范围的,为什么不使用包来提供防止与其他节点和叶子别名混淆的保险,并将它们简单地称为
Node
和Leaf
,同时离开它们在任何其他作用域构造(即对象)之外吗?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
andLeaf
, while leaving them outside any other scoping construct (i.e., an object)?我不建议将案例类放入其抽象超类中,因为嵌套类在 Scala 中是路径依赖的。如果有的话,您可以将它们放入伴随对象中。
(注:我还没有测试过这个......)
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.
(note: I haven't tested this...)