Scala 中的命名案例类

发布于 2024-09-01 00:05:50 字数 184 浏览 5 评论 0原文

我倾向于在案例类中使用这种冗余命名:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乱了心跳 2024-09-08 00:05:50

由于类、特征和对象名称是包范围的,为什么不使用包来提供防止与其他节点和叶子别名混淆的保险,并将它们简单地称为 NodeLeaf,同时离开它们在任何其他作用域构造(即对象)之外吗?

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)?

分开我的手 2024-09-08 00:05:50

我不建议将案例类放入其抽象超类中,因为嵌套类在 Scala 中是路径依赖的。如果有的话,您可以将它们放入伴随对象中。

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

(注:我还没有测试过这个......)

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...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文