List
has 2 methods that are specified to prepend an element to an (immutable) list:
+:
(implementing Seq.+:
), and
::
(defined only in List
)
+:
technically has a more general type signature—
def +:[B >: A, That](elem: B)(implicit bf: CanBuildFrom[List[A], B, That]): That
def ::[B >: A](x: B): List[B]
—but ignoring the implicit, which according to the doc message merely requires That
to be List[B]
, the signatures are equivalent.
What is the difference between List.+:
and List.::
? If they are in fact identical, I assume +:
would be preferred to avoid depending on the concrete implementation List
. But why was another public method defined, and when would client code call it?
Edit
There is also an extractor for ::
in pattern matching, but I'm wondering about these particular methods.
See also: Scala list concatenation, ::: vs ++
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…