I recently noticed some unexpected behavior and would appreciate your thoughts on whether this is intended or a bug.
In my app, I use classes that define constants to control various device-specific aspects. There is a base class that provides default values, and derived classes can override selected constants as needed.
This works fine when accessing the constants from instance methods. However, when I try to access them from a static method, I can only access constants that are explicitly defined in the concrete class. Inherited constants are not accessible.
Here is a simplified example:
import Toybox.Lang;class ConstantsA { public static const X as Number = 1; public static const Y as Number = 2; public static const Z as Number = 3;}class ConstantsB extends ConstantsA { public static const Y as Number = 2; public static const Z as Number = ConstantsA.Z;}class ConstantsC extends ConstantsA { public static const Y as Number = 2; public static const Z as Number = ConstantsB.Z;}class ConstantsTest { public function test() as Void { Toybox.System.println( ConstantsC.X ); // Works Toybox.System.println( ConstantsC.Y ); // Works Toybox.System.println( ConstantsC.Z ); // Works } public static function testStatic() as Void { Toybox.System.println( ConstantsC.X ); // Does not work Toybox.System.println( ConstantsC.Y ); // Works Toybox.System.println( ConstantsC.Z ); // Works }}
In this example, ConstantsC.X is accessible from the instance method but not from the static method, even though it is defined in the base class.
In the static method, I receive the following error:
ERROR: edge840: D:\GitHub\ohg\source\test.mc:26,8: Cannot find symbol ':X' on class definition '$.ConstantsC'.
Is there a logical explanation for this behavior? Or does this appear to be a bug in the language or compiler?
Any insight would be greatly appreciated.