Scala traits(特别是集合traits)的默认实现是如何工作的?

Scala traits(特别是集合traits)的默认实现是如何工作的?,scala,Scala,Range.map将始终返回IndexedSeq,IndexedSeq使用向量作为默认实现,因此Range.map将(始终?)返回向量。什么机制控制Range.map返回向量的方式?是什么使一个特征具有“默认”?用于为特征提供默认实现的模式是通过伴随对象工厂方法。为了说明模式,考虑下面的 trait Container class TreasureChest extends Container class BabySling extends Container class OfficeCubi

Range.map将始终返回IndexedSeq,IndexedSeq使用向量作为默认实现,因此Range.map将(始终?)返回向量。什么机制控制Range.map返回向量的方式?是什么使一个特征具有“默认”?

用于为特征提供默认实现的模式是通过伴随对象工厂方法。为了说明模式,考虑下面的

trait Container

class TreasureChest extends Container
class BabySling extends Container
class OfficeCubicle extends Container

object Container {
  def apply() = new OfficeCubicle // this decides what is the default implementation
}

Container() // or just sugar for Container.apply()
// res0: OfficeCubicle = OfficeCubicle@7c182b25
我们在这里看到,我们决定抽象
容器
的默认实现是
OfficeCubicle
。但这是一个设计决策,我们也可以选择默认的
BabySling


沿着这些线的模式在整个Scala集合中使用。

用于为trait提供默认实现的模式是通过伴随对象工厂方法。为了说明模式,考虑下面的

trait Container

class TreasureChest extends Container
class BabySling extends Container
class OfficeCubicle extends Container

object Container {
  def apply() = new OfficeCubicle // this decides what is the default implementation
}

Container() // or just sugar for Container.apply()
// res0: OfficeCubicle = OfficeCubicle@7c182b25
我们在这里看到,我们决定抽象
容器
的默认实现是
OfficeCubicle
。但这是一个设计决策,我们也可以选择默认的
BabySling


沿着这些线的模式在整个Scala集合中使用。

这与Scala集合的factory特性有关吗?这与Scala集合的factory特性有关吗?