Acknowledged

sdk-6.2.2 changes lookup rules for class statics

This code:

module MyMod {
    const ModConst = 2;
    class Base {
        static const BaseConst = 3;
    }
}

class Test extends MyMod.Base {
    function initialize() {
        Base.initialize();
    }
    static const TestOne = ENDIAN_BIG;
    static const TestTwo = ModConst;
    static const TestThree = BaseConst;
}

function test() {
  System.println(Test.TestOne);
  System.println(Test.TestTwo);
  System.println(Test.TestThree);
}

Compiles (even with Strict type checking) and runs as you might expect prior to sdk-6.2.2. ie

  • TestOne is assigned the value of Lang.ENDIAN_BIG (it finds it by following the inheritance chain to MyMod.Base, then Lang.Object, and then searches out from Lang.Object to find Lang.ENDIAN_BIG).
  • TestTwo is assigned the value of MyMod.ModConst (again, it follows the inheritance chain to MyMod.Base, and then searches out to MyMod)
  • TestThree is assigned the value of Base.BaseConst.

With the default optimization level, you can see (via -g) that the values have been substituted at compile time, so no runtime lookups actually happen. But if you set optimization level to 0, no substitutions happen, and it's clear that all the lookups happen at runtime, and work.

With sdk-6.2.2 the type checker complains that ENDIAN_BIG, ModConst and BaseConst don't exist, and refuses to compile. If you turn the type checker off, it then compiles, but crashes at runtime because the symbols don't exist (and I've verified by testing each of the three cases separately that all of them fail).

So the runtime has been changed, and the type checker has been updated to match - making me think this is an intentional change. But it breaks existing code, and its not clear what it might fix...

Also note that similar code without "static" continues to work.

Parents
  • What's static vs non static is supposed to mean for const?

    There is one other difference, which is that for non-static consts, the initializer is evaluated whenever an instance of the class is created, while static consts are initialized once early on. So eg:

    var x as Number = 42;
    class Strange {
      const y = x;
    }
    
    function test() {
      var s1 = new Strange();
      System.println(s1.y); // prints 42 (the first time test is called)
      x++;
      var s2 = new Strange();
      System.println(s2.y); // prints 43
    }
    

Comment
  • What's static vs non static is supposed to mean for const?

    There is one other difference, which is that for non-static consts, the initializer is evaluated whenever an instance of the class is created, while static consts are initialized once early on. So eg:

    var x as Number = 42;
    class Strange {
      const y = x;
    }
    
    function test() {
      var s1 = new Strange();
      System.println(s1.y); // prints 42 (the first time test is called)
      x++;
      var s2 = new Strange();
      System.println(s2.y); // prints 43
    }
    

Children
No Data