+= on a Vector gives strange / wrong type errors

Viewed 499

I have a variable v that is a Vector, and I'm trying to add an element to it using +=. It complains that it expects a String instead of an Int:

Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45).
Type in expressions to have them evaluated.
Type :help for more information.

scala> var v = Vector[Int]()
v: scala.collection.immutable.Vector[Int] = Vector()

scala> v += 3
<console>:9: error: type mismatch;
 found   : Int(3)
 required: String
              v += 3
                   ^

Why does it expect a String? When I give it a String (which is ofcourse wrong), it says it expects a Vector[Int]:

scala> v += "three"
<console>:9: error: type mismatch;
 found   : String
 required: scala.collection.immutable.Vector[Int]
              v += "three"
                ^

And when I give it a Vector[Int], it again expects a String:

scala> v += Vector(3)
<console>:9: error: type mismatch;
 found   : scala.collection.immutable.Vector[Int]
 required: String
              v += Vector(3)
                         ^

Why does this happen?

I know I can add an element using +:=. But why can I not use +=, like for a Set?

1 Answers
Related