Scala list concatenation, ::: vs ++

Is there any difference between ::: and ++ for concatenating lists in Scala?

scala> List(1,2,3) ++ List(4,5)
res0: List[Int] = List(1, 2, 3, 4, 5)

scala> List(1,2,3) ::: List(4,5)
res1: List[Int] = List(1, 2, 3, 4, 5)

scala> res0 == res1
res2: Boolean = true

From the documentation it looks like ++ is more general whereas ::: is List-specific. Is the latter provided because it’s used in other functional languages?

4 Answers
4

Leave a Comment