Why no StringBuilder.+=(String) in Scala?

Tag: scala , types Author: wowokaokaokao Date: 2012-07-08

This behavior seems to be broken (I am using Scala 2.9.1)

var b= new StringBuilder

These are OK:


This produces compile error:


Any idea as to why only StringBuilder#+=(c: Char) exists whereas both StringBuilder#append(c:Char) and StringBuilder#append(s:String) happily co-exist? What is wrong with declaring and implementing StringBuilder#+=(s: String)?

Is it oversight or some deeper problem in the Scala type system?

Best Answer

Try b ++= "de". A String is considered a collection of Chars.


Thanks for the tip. ++= is bearable, I still would hope that Scala type system is intelligent enough to tell String from Char ;-)
It is, that's why you have to use a different method.
I have edited the question to hopefully make it clearer. My point is that I don't want to use a different method (or remember that I have to use different method in this situation). This is why I think it is broken.
It is done this way on purpose.
As far as I can tell, in 2.9, ++= is defined in a generic way in Growable as def ++=(xs: TraversableOnce[A]): this.type = { xs.seq foreach += ; this }, which might not be very efficient. But Scala 2.10's StringBuilder overrides ++= for efficiency.