2
我一直在Scala代码中进行挖掘。它看起来像TreeMap
使用以下构建器类。地图构建器每次都从头开始重新创建地图?
class MapBuilder[A, B, Coll <: scala.collection.GenMap[A, B] with scala.collection.GenMapLike[A, B, Coll]](empty: Coll)
extends Builder[(A, B), Coll] {
protected var elems: Coll = empty
def +=(x: (A, B)): this.type = {
elems = (elems + x).asInstanceOf[Coll]
// the cast is necessary because right now we cannot enforce statically that
// for every map of type Coll, `+` yields again a Coll. With better support
// for hk-types we might be able to enforce this in the future, though.
this
}
def clear() { elems = empty }
def result: Coll = elems
}
我不明白演员,但除此之外。例如,当两个TreeMap
s是++
- 合在一起时,实例化新的TreeMap
,然后添加来自TreeMap
s的所有键值对。由于TreeMap
是不可变的,我们为什么不能从TreeMap
之一开始,只是添加其他项目?这是因为++
适用于不可变和可变类型,所以我们需要制作一个防御副本,并且一些集合可能有更有效的策略?
你是对的 - 我不知道我是如何错过的。想到更多的建设者可能是这样写的,所以它可以处理地图,过滤器等。 – schmmd 2012-03-15 16:20:48
正确的 - 并转换为不同的集合。 – schmmd 2012-03-15 16:43:51