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

abstract val fields of value classes

3 replies
Avi Pfeffer
Joined: 2009-02-23,
User offline. Last seen 42 years 45 weeks ago.

Hi all,

Could someone please explain this behavior?

> scala
Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Client VM, Java
1.6.0_05).
Type in expressions to have them evaluated.
Type :help for more information.

scala> abstract class Foo {
| val x : Int
| val y : List[Int]
| println(x)
| println(y)
| }
defined class Foo

scala> class Bar extends Foo {
| val x = 9
| val y = List(1,2,3)
| }
defined class Bar

scala> new Bar
9
null
res0: Bar = Bar@1fbe226

I understand why y prints as null, but why does x print as 9 and not 0?

Avi

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: abstract val fields of value classes

I would ask 'fakert' - since y is abstract, why is y ever == to null?

On 2/23/09, Avi Pfeffer wrote:
> Hi all,
>
> Could someone please explain this behavior?
>
>> scala
> Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Client VM, Java
> 1.6.0_05).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> abstract class Foo {
> | val x : Int
> | val y : List[Int]
> | println(x)
> | println(y)
> | }
> defined class Foo
>
> scala> class Bar extends Foo {
> | val x = 9
> | val y = List(1,2,3)
> | }
> defined class Bar
>
> scala> new Bar
> 9
> null
> res0: Bar = Bar@1fbe226
>
> I understand why y prints as null, but why does x print as 9 and not 0?
>
> Avi
>

Alex Boisvert
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: abstract val fields of value classes
Pre-initialization allows you to initialize the subclass before the superclass is called,

class Bar extends {
 val x = 9
 val y = List(1,2,3)
} with Foo  

alex


On Mon, Feb 23, 2009 at 9:41 AM, Avi Pfeffer <avi@eecs.harvard.edu> wrote:
Hi all,

Could someone please explain this behavior?

> scala
Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Client VM, Java
1.6.0_05).
Type in expressions to have them evaluated.
Type :help for more information.

scala> abstract class Foo {
    | val x : Int
    | val y : List[Int]
    | println(x)
    | println(y)
    | }
defined class Foo

scala> class Bar extends Foo {
    | val x = 9
    | val y = List(1,2,3)
    | }
defined class Bar

scala> new Bar
9
null
res0: Bar = Bar@1fbe226

I understand why y prints as null, but why does x print as 9 and not 0?

Avi

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: abstract val fields of value classes

On Mon, Feb 23, 2009 at 6:41 PM, Avi Pfeffer wrote:
> Hi all,
>
> Could someone please explain this behavior?
>
>> scala
> Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Client VM, Java
> 1.6.0_05).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> abstract class Foo {
>     | val x : Int
>     | val y : List[Int]
>     | println(x)
>     | println(y)
>     | }
> defined class Foo
>
> scala> class Bar extends Foo {
>     | val x = 9
>     | val y = List(1,2,3)
>     | }
> defined class Bar
>
> scala> new Bar
> 9
> null
> res0: Bar = Bar@1fbe226
>
> I understand why y prints as null, but why does x print as 9 and not 0?
>
This is a relic from past times. The Scala compiler used to
automatically move a val definition which did not reference `this' in
front of the superclass constructor call. This ability was needed to
make some patterns of component composition work. Some time ago, we
decided that this was too fragile, and introduced early definition
syntax instead.

However, we could not remove the ``feature'' until now, because it
might break existing code. We will however remove it in 2.8. You can
already verify this now by compiling with command line option
-Xfuture. If you want to check whether your code will break by the
change, there's another option, -Xcheckinit.

Hope this helps

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