Aladdin - Scala Bugtracking
[#429] project: compiler priority: low category: bug
submitter assigned to status date submitted
Philippe Martin fixed 2005-05-03 14:09:40.0
subject The analyzer transforms constant fields into constant functions
code
object Test {
  abstract class A {
    Console.print("A");
    val x: Int;
    val y: Int = {Console.print("y"); x + 1}
  }
  class B extends A {
    Console.print("B");
    val x = 4;
  }
  def main (args: Array[String]): Unit = {
    Console.print((new B).y);
  }
}
what happened
The program prints AyB5.
what expected

It should print AyB1. The problem is caused by the interpreter which infers that B.x has the constant type Int(4) and transforms therefore the field val x = 4 into the function def x: Int(4) = 4. The analyzer should not do this transformation because it changes the runtime behaviour of the class B.

[back to overview]
Changes of this bug report
Martin  edited on  2006-03-30 15:19:12.0
The reason why it prints this is actually a different one: Because the definition of `x' does not refer to `this' it is move to before the superclass constructor call. If we change the definition of `x' to val z = 0; val x = 4 + z Then AyB1 will be printed. It's probably still a bug though, we should not move value definitions unless they are proxies for parameters. -- Martin
Martin  edited on  2006-05-27 11:38:52.0