how does += work

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

how does += work

by Himanshu-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


scala> var x = 10
x: Int = 10

scala> x += 2

scala> x
res12: Int = 12

How does += work when It is neither a method defined in Int nor in RichInt ?

It seems that one can use any method name suffixed with '=' to do assignment but its not clear to me how its happening.

Thanks,
Himanshu

Re: how does += work

by David Hall-17 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Nov 7, 2009 at 1:02 AM, Himanshu <g.himanshu@...> wrote:

>
> scala> var x = 10
> x: Int = 10
>
> scala> x += 2
>
> scala> x
> res12: Int = 12
>
> How does += work when It is neither a method defined in Int nor in RichInt ?

Scala has special handling of operators of the form "?=", where ? is
an operator. Specifically, if there is no ?= method on the LHS, and
there is no implicit conversion to an object that has a ?= method,
then the expression is treated as "x = x ? y" (but only if that would
compile...)

This means that you can define +, and get += (on vars) for free.

-- David

>
> It seems that one can use any method name suffixed with '=' to do assignment
> but its not clear to me how its happening.
>
> Thanks,
> Himanshu
>

Parent Message unknown Re: how does += work

by Himanshu-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

sorry, forgot cc'ing scala-user...

On Sat, Nov 7, 2009 at 3:33 PM, Himanshu <g.himanshu@...> wrote:
Thanks David, But why, then following doesn't work...

scala> class A(var x: Int) { def op(y:Int) = new A(y) }
defined class A

scala> var a = new A(10)
a: A = A@914272

scala> a op= 5                                //Note that this does not work, even though it should be same as a = a op 5
<console>:6: error: reassignment to val
       a op= 5
           ^

scala> a = a op 5                           //and indeed a =  a op 5 works
a: A = A@7691c0

scala> a.x
res15: Int = 5




On Sat, Nov 7, 2009 at 2:41 PM, David Hall <dlwh@...> wrote:
On Sat, Nov 7, 2009 at 1:02 AM, Himanshu <g.himanshu@...> wrote:
>
> scala> var x = 10
> x: Int = 10
>
> scala> x += 2
>
> scala> x
> res12: Int = 12
>
> How does += work when It is neither a method defined in Int nor in RichInt ?

Scala has special handling of operators of the form "?=", where ? is
an operator. Specifically, if there is no ?= method on the LHS, and
there is no implicit conversion to an object that has a ?= method,
then the expression is treated as "x = x ? y" (but only if that would
compile...)

This means that you can define +, and get += (on vars) for free.

-- David

>
> It seems that one can use any method name suffixed with '=' to do assignment
> but its not clear to me how its happening.
>
> Thanks,
> Himanshu
>



Parent Message unknown Re: how does += work

by Himanshu-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks. But looks like my earlier response was not sent properly, anyway here it is again..


>>>
>>> Scala has special handling of operators of the form "?=", where ? is
>>> an operator. Specifically, if there is no ?= method on the LHS, and
>>> there is no implicit conversion to an object that has a ?= method,
>>> then the expression is treated as "x = x ? y" (but only if that would
>>> compile...)



> Scala's rules for lexing splits up "op=" into two tokens, and then
> attempts to compile (a op) = 5. It won't if "op" is replaced with a
> sequence of operator characters.

 
Can we explain why following is not working?

scala> class A(var x: Int) { def m(y:Int) = new A(y) }
defined class A

scala> var a = new A(10)
a: A = A@1ca6954

scala> a m= 5            //Notice, this does not work
<console>:8: error: reassignment to val
       a m= 5
          ^

scala> (a m) = 5         //This does not work either
<console>:1: error: ';' expected but '=' found.
       (a m) = 5
             ^

scala> a = a m 5       //But this does.
a: A = A@d1e32d

Re: how does += work

by Paul Phillips-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Nov 07, 2009 at 07:01:58PM +0530, Himanshu wrote:
> scala> var a = new A(10)
> a: A = A@1ca6954
>
> scala> a m= 5            *//Notice, this does not work*
> <console>:8: error: reassignment to val
>        a m= 5
>           ^

m= is not even a legal token.  Operators can only be composed of
operator characters (see the specification) or if you want to mix
alphanumerics, they have to be underscore separated liked foo_++.

--
Paul Phillips      | Every election is a sort of advance auction sale
Future Perfect     | of stolen goods.
Empiricist         |     -- H. L. Mencken
pull his pi pal!   |----------* http://www.improving.org/paulp/ *----------

Re: how does += work

by Kevin Wright-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Excellent!
Yet another use for our friend the underscore :)

On Sat, Nov 7, 2009 at 3:13 PM, Paul Phillips <paulp@...> wrote:

> On Sat, Nov 07, 2009 at 07:01:58PM +0530, Himanshu wrote:
>> scala> var a = new A(10)
>> a: A = A@1ca6954
>>
>> scala> a m= 5            *//Notice, this does not work*
>> <console>:8: error: reassignment to val
>>        a m= 5
>>           ^
>
> m= is not even a legal token.  Operators can only be composed of
> operator characters (see the specification) or if you want to mix
> alphanumerics, they have to be underscore separated liked foo_++.
>
> --
> Paul Phillips      | Every election is a sort of advance auction sale
> Future Perfect     | of stolen goods.
> Empiricist         |     -- H. L. Mencken
> pull his pi pal!   |----------* http://www.improving.org/paulp/ *----------
>

Re: how does += work

by Sean Corfield :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That made me wonder whether the following would work (it doesn't):

scala> class A(var x: Int) { def m_(y:Int) = new A(y) }
defined class A

scala> var a = new A(10)
a: A = A@57baae

scala> a = a m_ 5
a: A = A@6d6c90

scala> a m_= 5
<console>:7: error: value m_= is not a member of A
       a m_= 5
         ^

scala>

(m_= is not an 'operator' so the = magic doesn't apply)

On Sat, Nov 7, 2009 at 7:13 AM, Paul Phillips <paulp@...> wrote:

> On Sat, Nov 07, 2009 at 07:01:58PM +0530, Himanshu wrote:
>> scala> var a = new A(10)
>> a: A = A@1ca6954
>>
>> scala> a m= 5            *//Notice, this does not work*
>> <console>:8: error: reassignment to val
>>        a m= 5
>>           ^
>
> m= is not even a legal token.  Operators can only be composed of
> operator characters (see the specification) or if you want to mix
> alphanumerics, they have to be underscore separated liked foo_++.