Given the two lists aand bwhat is the difference between a ::: band a ++ b? I suspected that one of these statements would simply call the other, but in fact the implementations look completely different:
def :::[B >: A](prefix: List[B]): List[B] =
if (isEmpty) prefix
else if (prefix.isEmpty) this
else (new ListBuffer[B] ++= prefix).prependToList(this)
override def ++[B >: A, That](that: GenTraversableOnce[B])
(implicit bf: CanBuildFrom[List[A], B, That]): That = {
val b = bf(this)
if (b.isInstanceOf[ListBuffer[_]])(this ::: that.seq.toList).asInstanceOf[That]
else super.++(that)
}
In terms of use, do I prefer a ::: bor a ++ b? From an implementation point of view, is there a specific reason why one of these operators does not just call the other?
source
share