gpt4 book ai didi

scala - 为什么所有基本的不可变集合都是最终的或密封在scala中的?

转载 作者:行者123 更新时间:2023-12-04 03:59:51 24 4
gpt4 key购买 nike

我在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].

最佳答案

因为如果我写

someList match {
case Cons(head, tail) => whatever
case Nil => somethingElse
}

我不希望我的逻辑被新的意外子类破坏。

我怀疑您正在尝试使用子类来解决问题,而使用子类可以更好地使用隐式方法解决。

关于scala - 为什么所有基本的不可变集合都是最终的或密封在scala中的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8947801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com