- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
abstract val fields of value classes
Mon, 2009-02-23, 18:45
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
Mon, 2009-02-23, 20:47
#2
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:
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
Mon, 2009-02-23, 20:57
#3
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
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
>