为什么所有基本不可变集合都是最终的或密封在 scala 中?
我在 Scala 中编写了一个用于对可迭代对象进行排序的特征,并且创建一个可以将其混合在一起的类所花费的时间几乎与编写该特征所花费的时间一样长。为什么设计决定不允许用户编写如下内容:
new List[Int] with SuperAwesomeTrait[Int]?
现在,如果我想这样做,我需要做一些奇怪的黑客,
class StupidList extends LinearSeq {
val inner = List()
/* reimplement list methods by calling into inner */
}
然后
new StupidList[Int] with SuperAwesomeTrait[Int].
I wrote a trait for sorting iterables in Scala, and it took almost as long to make a class that I could mix it in with as it took to write the trait. Why was the design decision made to disallow users from writing things like:
new List[Int] with SuperAwesomeTrait[Int]?
Now if I want to do this, I need to do some weird hack like,
class StupidList extends LinearSeq {
val inner = List()
/* reimplement list methods by calling into inner */
}
and then
new StupidList[Int] with SuperAwesomeTrait[Int].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为如果我写,
我不希望我的逻辑被一个新的、意想不到的子类破坏。
我怀疑您正在尝试使用子类化来解决问题,而使用隐式可以更好地解决该问题。
Because if I write
I don't want my logic broken by a new, unexpected subclass.
I suspect you're trying to solve a problem using subclassing that would be better solved with implicits.