This page is no longer maintained — Please continue to the home page at www.scala-lang.org

var constructor parameters

6 replies
Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.

Over at http://scala-forum.org/read.php?4,1399 this example came up:

class A(var x:Int) {
println("x=" + x)
x=99
println("x is now equal to " + x)
}
val test = new A(10)

resulting in:

x=10
x is now equal to 10

Not what the poster expected, and not what I expected. Can this be
according to spec somehow, or is it just a bug?

The behavior is the same in both 2.7 and 2.8.

Colin Bullock
Joined: 2009-01-23,
User offline. Last seen 42 years 45 weeks ago.
Re: var constructor parameters
It looks to be a bug (to me). The problem is not in the assignment, but in the 2nd println statement, as can be seen in this snippet decompiled from the bytecode:

    public A(int x)
    {
        this.x = x;
        super();
        Predef$.MODULE$.println(BoxesRunTime.boxToInteger(x));
        x_$eq(99);
        Predef$.MODULE$.println(BoxesRunTime.boxToInteger(x)); // Field shadowed by param
    }

- Colin
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: var constructor parameters

On Tue, May 18, 2010 at 12:47:45PM -0500, Colin Bullock wrote:
> It looks to be a bug (to me).

If that's not a bug I will eat my hat.

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: var constructor parameters


On 18 May 2010 20:22, Paul Phillips <paulp@improving.org> wrote:
On Tue, May 18, 2010 at 12:47:45PM -0500, Colin Bullock wrote:
> It looks to be a bug (to me).

If that's not a bug I will eat my hat.

I bet it's a stetson too!  There's some good eating in those... 

--
Paul Phillips      | Simplicity and elegance are unpopular because
Everyman           | they require hard work and discipline to achieve
Empiricist         | and education to be appreciated.
i pull his palp!   |     -- Dijkstra



--
Kevin Wright

mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: var constructor parameters


On Tue, May 18, 2010 at 12:50 PM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:


On 18 May 2010 20:22, Paul Phillips <paulp@improving.org> wrote:
On Tue, May 18, 2010 at 12:47:45PM -0500, Colin Bullock wrote:
> It looks to be a bug (to me).

If that's not a bug I will eat my hat.

I bet it's a stetson too!  There's some good eating in those...


I wonder if this is Paul's secret life: http://www.hatworksbypaul.com/
 
 

--
Paul Phillips      | Simplicity and elegance are unpopular because
Everyman           | they require hard work and discipline to achieve
Empiricist         | and education to be appreciated.
i pull his palp!   |     -- Dijkstra



--
Kevin Wright

mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: var constructor parameters


On 18 May 2010 21:05, David Pollak <feeder.of.the.bears@gmail.com> wrote:


On Tue, May 18, 2010 at 12:50 PM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:


On 18 May 2010 20:22, Paul Phillips <paulp@improving.org> wrote:
On Tue, May 18, 2010 at 12:47:45PM -0500, Colin Bullock wrote:
> It looks to be a bug (to me).

If that's not a bug I will eat my hat.

I bet it's a stetson too!  There's some good eating in those...


I wonder if this is Paul's secret life: http://www.hatworksbypaul.com/


How long did you spend searching for that? :P
 
 

--
Paul Phillips      | Simplicity and elegance are unpopular because
Everyman           | they require hard work and discipline to achieve
Empiricist         | and education to be appreciated.
i pull his palp!   |     -- Dijkstra



--
Kevin Wright

mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics



--
Kevin Wright

mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: var constructor parameters


On Tue, May 18, 2010 at 1:25 PM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:


On 18 May 2010 21:05, David Pollak <feeder.of.the.bears@gmail.com> wrote:


On Tue, May 18, 2010 at 12:50 PM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:


On 18 May 2010 20:22, Paul Phillips <paulp@improving.org> wrote:
On Tue, May 18, 2010 at 12:47:45PM -0500, Colin Bullock wrote:
> It looks to be a bug (to me).

If that's not a bug I will eat my hat.

I bet it's a stetson too!  There's some good eating in those...


I wonder if this is Paul's secret life: http://www.hatworksbypaul.com/


How long did you spend searching for that? :P

It's 2 blocks from my house.  ;-) 

 
 

--
Paul Phillips      | Simplicity and elegance are unpopular because
Everyman           | they require hard work and discipline to achieve
Empiricist         | and education to be appreciated.
i pull his palp!   |     -- Dijkstra



--
Kevin Wright

mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics



--
Kevin Wright

mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland