|
View:
New views
7 Messages
—
Rating Filter:
Alert me
|
|
|
how does += workscala> 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 += workOn 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 > |
|
|
|
|
|
|
|
|
Re: how does += workOn 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 += workExcellent!
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 += workThat 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_++. |
| Free embeddable forum powered by Nabble | Forum Help |