Aladdin - Scala Bugtracking
[#17] project: compiler priority: medium category: bug
submitter assigned to status date submitted
Stephane Nikolay fixed 2003-06-26 11:52:54.0
subject initialization of class parameters
code
class Quantity {
    def getValue = 0;
    def connect(c: Constraint) = c.newValue;
}

abstract class Constraint(q: Quantity) {
    def newValue: Unit;
    q connect this
}

class Adder(q: Quantity) extends Constraint(q) {
    def newValue = System.out.println(q.getValue);
}

object Main {
    def main(args: Array[String]): Unit = {
        val x = new Quantity;
        new Adder(x);
        ()
    }
}
what happened
Exception in thread "main" java.lang.NullPointerException
        at Adder$class.newValue(src/Main.scala)
what expected value 0 should be printed out !

Michel

the problem is that the super-constructor is called before the fields of the class are initialized. I think addconstructors should be modified in order to place the call to the superconstructor just after all assignments to local fields

[back to overview]
Changes of this bug report
Nikolay  edited on  2003-06-26 11:52:54.0
Moved the initialzation of the class-parameters fields before the call to the super constructor. Regular ValDefs initialization is done after that. This still leaves room for initialization cycles but placing them before the call to the superconstructor breaks a much more common usage pattern, e.g. class A { val a = 1; } class B extends A { val b = a; System.out.println(b); } doesn't even pass the verifier.