What's the difference between `::` and `+:` for prepending to a list)?

前端 未结 2 515
孤街浪徒
孤街浪徒 2020-12-14 06:06

List has 2 methods that are specified to prepend an element to an (immutable) list:

  • +: (implementing Seq.+:), and
  • <
2条回答
  •  太阳男子
    2020-12-14 06:39

    +: is more generic, since it allows the result type to be different from the type of the object it is called on. For example:

    scala> Range(1,4).+:(0)
    res7: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 1, 2, 3)
    

提交回复
热议问题