What is the difference between: and ++ for lists?

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?

+5
source share
1 answer

The difference is that you can use only :::in 2 lists - this operation is available only in the type List. Since lists are sequences, it acts as a concatenation operator for lists.

++ - . , , .

++ ::: 2 - ::: - ++ , .

if, ++, - this that , :::, , ++, this that that.

, - . ++ - .

+10

All Articles