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

Change in construction order

1 reply
ewilligers
Joined: 2008-08-20,
User offline. Last seen 3 years 17 weeks ago.

The following code succeeds with existing Scala releases, but gives a
NullPointerException with recent nightly builds for 2.8.0

trait A {
def f: String
}

abstract class B extends A {
val v = f.length
}

object C extends B with Application {
// def f = "" // succeeds
// lazy val f = "" // succeeds
val f = "" // NullPointerException in B.
}

In C's constructor, the putfield for f is now being performed after
calling the superclass constructor, instead of before. (Bytecode attached)

Is this intended?

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Change in construction order

On Thu, Jan 22, 2009 at 11:02:32AM +1100, Eric Willigers wrote:
> The following code succeeds with existing Scala releases, but gives a
> NullPointerException with recent nightly builds for 2.8.0

And you can actually get a much more explanatory message than is usually available, the catch
being that you have to know to compile with -Xexperimental.

$ scalac27 -Xexperimental a.scala
a.scala:12: warning: the semantics of this definition has changed;
the initialization is no longer be executed before the superclass is called
val f = "" // NullPointerException in B.
^
one warning found

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