« Return to Thread: += and +

+= and +

by Eric Willigers :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View in Thread

What do we expect the following to print?

     var s = "A"
     s += "B" + "C"
     println(s)

ABC?

Do we expect the following to compile?

     false + "D"
     () + "E"

No, No?

Actually it prints AB, and they do compile.

Here's what  scalac -Xprint:cleanup  tells us:-

s = "A";
{
   scala.this.Predef.any2stringadd({
     s_=(s().+("B"));
     scala.runtime.BoxedUnit.UNIT
   })
}.+("C");
scala.this.Predef.println(s());
scala.this.Predef.any2stringadd(scala.Boolean.box(false)).+("D");
scala.this.Predef.any2stringadd(scala.runtime.BoxedUnit.UNIT).+("E");



any2stringadd isn't mentioned in the language spec.  What is its
motivation? To allow  any + string  just as  string + any  is allowed?

It is nice to be able to write  println(n + " ...")

I'll need to remember to use parens:  s += ("B" + "C")

 « Return to Thread: += and +