+= and +

View: New views
6 Messages — Rating Filter:   Alert me  

+= and +

by Eric Willigers :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

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")


Re: += and +

by csar :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message



On Jan 10, 2008 12:16 PM, Eric Willigers <ewilligers@...> wrote:
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?
Agree, this is what we see in other (statically typed) languages
 

It is nice to be able to write  println(n + " ...")
I agree that it is nice but this is why we have the strange effects above due to the globally available any2stringad
d - with infix constructors this could be done much cleaner 


I'll need to remember to use parens:  s += ("B" + "C")
This horrible as val  s = "B"+"C" works fine.



Re: += and +

by Jamie Webb-2 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On 2008-01-10 22:16:37 Eric Willigers wrote:
> What do we expect the following to print?
>
>      var s = "A"
>      s += "B" + "C"
>      println(s)
>
> ABC?

See issue #92.

/J

Re: += and +

by Franco Lombardo :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

>>   var s = "A"
>>   s += "B" + "C"
>>   println(s)
> See issue #92.

Please, can you tell me what is issue #92?
And is there anyone who can explain me the strange behaviour of s += "B" +
"C"?
Thanks a lot.

Bye

Franco




Re: Re: += and +

by Jamie Webb-2 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On 2008-01-10 14:09:24 Franco Lombardo wrote:
> >>   var s = "A"
> >>   s += "B" + "C"
> >>   println(s)
> > See issue #92.
>
> Please, can you tell me what is issue #92?
> And is there anyone who can explain me the strange behaviour of s +=
> "B" + "C"?

A ticket in the Scala bug tracker:

  http://lampsvn.epfl.ch/trac/scala/ticket/92

The behaviour is thanks to Scala's precedence rules. The above
parenthesises as:

  (s += "B") + "C"

That is, "B" is appended to s and the result stored back in s, and then
"C" is appended but that result is discarded.

/J

Re: Re: += and +

by Franco Lombardo :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message


"Jamie Webb" <j@...> wrote

>...
>   http://lampsvn.epfl.ch/trac/scala/ticket/92
>
> The behaviour is thanks to Scala's precedence rules. The above
> parenthesises as:
>
>   (s += "B") + "C"
>
> That is, "B" is appended to s and the result stored back in s, and then
> "C" is appended but that result is discarded.

Now I understand it. In my poor opinion, this is for sure an issue to fix!

Thanks a lot.

Bye

Franco