I think the scala docs explains it pretty well.
A copy of this list with an element appended.
scala> acc
res3: List[Bit] = List()
scala> acc :+ 1
res4: List[Int] = List(1)
scala> acc
res5: List[Bit] = List()
Basically when you do acc:+1
it creates a new list copying elements of acc
and adding 1
to it. No change is made to acc
.
Well I think its a typo. I think you means ("ins" :: acc ):+1
scala> "ins"+acc:+1
res8: scala.collection.immutable.IndexedSeq[AnyVal] = Vector(i, n, s, L, i, s, t
, (, ), 1)
scala> "ins":+acc:+1
res9: scala.collection.immutable.IndexedSeq[Any] = Vector(i, n, s, List(), 1)
scala> ("ins" :: acc) :+ 1
res10: List[Any] = List(ins, 1)
"ins"+acc:+1
prints as shown because String in scala implicitly is an IndexedSeq
. Doing "ins"+acc is basically "ins"+acc.toString
which is "insList()"
. Now "insList()"
is of type IndexedSeq[Char]
. Doing :+ 1
to it will make it convert to IndexedSeq[AnyVal]
as (String <: AnyVal
and Int <:AnyVal
). Hence it prints a sequence as shown.