Scala 深入浅出实战经典第 83讲:Scala中List的实现内幕源码揭秘

2.10.x 版本 List中的take是用ListBuffer实现的。
但是在2.11.x版本中不是:
 override def take (n: Int): List[ A] = if (isEmpty || n <= 0) Nil else {
    val h = new ::( head, Nil )
    var t = h
    var rest = tail
    var i = 1
    while ({ if (rest .isEmpty) return this ; i < n}) {
      i += 1
      val nx = new ::(rest .head, Nil)
      t.tl = nx
      t = nx
      rest = rest .tail
    }
    h
  }

final case class ::[B](override val head : B, private [scala] var tl: List[B]) extends List[B] 
声明为var是可以让ListBuffer操作


信息来源于 DT大数据梦工厂,微信公众号:DT_Spark 
视频地址:http://edu.51cto.com/lesson/id-71363.html

你可能感兴趣的:(scala)