为什么选择不直接扩展了Iterable特质?
-
25-09-2019 - |
题
Option
隐式转换为一个Iterable
- 但为什么它不只是只实现Iterable
直接:
def iterator = new Iterator[A] {
var end = !isDefined
def next() = {
val n = if (end) throw new NoSuchElementException() else get
end = true
n
}
def hasNext = !end
}
修改强> 实际上它甚至比这韦德因为在2.8 Option
不声明一个iterator
方法:
def iterator: Iterator[A] =
if (isEmpty) Iterator.empty else Iterator.single(this.get)
解决方案
我在想,有太多会被要求非荒谬的方法。例如,你会期待的返回值是:
Some(1) ++ Some(2)
此目前编译并在2.8经由implicits评估为列表(1,2),但似乎奇怪。
也许这就是为什么说2.7的文档注释:
Only potentially unbounded collections should directly sub-class Iterable
编辑:作为@下面将mattr的评论,我离开了文档注释建议分型集合可能误导出。而考虑到它摇身一变这个问题为“为什么不选择延长征收特质?”
不隶属于 StackOverflow