在关联对象定义

时间:2017-07-14 12:49:12

标签: scala

我正在阅读Odersky和Spoon的article。 所以这里有一个代码清单,从这篇文章中有一个特殊的方面:

import scala.collection.mutable.{Builder, MapBuilder}
import scala.collection.generic.CanBuildFrom

object PrefixMap extends {
  def empty[T] = new PrefixMap[T]

  def apply[T](kvs: (String, T)*): PrefixMap[T] = {
    val m: PrefixMap[T] = empty
    for (kv <- kvs) m += kv
    m
  }

  def newBuilder[T]: Builder[(String, T), PrefixMap[T]] = 
    new MapBuilder[String, T, PrefixMap[T]](empty)

  implicit def canBuildFrom[T]
    : CanBuildFrom[PrefixMap[_], (String, T), PrefixMap[T]] = 
      new CanBuildFrom[PrefixMap[_], (String, T), PrefixMap[T]] {
        def apply(from: PrefixMap[_]) = newBuilder[T]
        def apply() = newBuilder[T]
      }
}

为什么他们将伴侣对象声明为object PrefixMap extends {...}而不仅仅是object PrefixMap {...}? 你能暗示在这种情况下延伸的目的吗?

0 个答案:

没有答案