Enum's

Hi,

I can't seem to access an enum from another class (in the same project). For example:
class myClass
{
static enum { MY_CONSTANT }
}

class myView
{
var status = myClass.MY_CONSTANT;
}


This fails at runtime with 'Symbol Not Found'

What am I doing wrong?

Cheers
Chris
  • I think should work, but I'm able to reproduce the problem. I think we may already have a more general case open that covers this specific issue, but I'll report it separately to make sure we get it fixed.

    You can declare the enum at the module level as a workaround.
  • Thanks Brandon - at least I am not going wrong!
  • It seems to me that your declaration is invalid, and should probably be rejected by the compiler. It does not make sense to declare the enum type as static as you are declaring a type (and not data). Does it work when you remove that?

    Travis
  • No, I did try without static.

    I thought enum are (see below) constants, and not types (I.e you can't declare a type of enum)?

    Static makes sense for not instantiating, as I shouldn't need an instance to utilise.

    Cheers
    Chris

    From documentation "Enumerations are explicit or auto-incrementing constant mappings from symbol to integer"
  • I wasn't sure about the use of 'static' myself, but it doesn't work without 'static' either--I tried several different variations. I reported it as-is so that the team (including the creator of the language) can look it over and decide what to do with this. ;)
  • I agree static makes no sense in a 'proper' enum (as a type) context.

    However if (as per the documentation) enum's are numbered constants, then they do... as they would instantiate per object instance?
  • However if (as per the documentation) enum's are numbered constants, then they do... as they would instantiate per object instance?

    I'm probably taking too much from my understanding of other languages (C/C++/Java). In those languages an enum declares a type that is implicitly static. I did test, and the enum values are actually of type Lang.Number. I also found that they are not implicitly static.

    using Toybox.Application as App;
    using Toybox.Lang as Lang;
    using Toybox.System as Sys;
    using Toybox.WatchUi as Ui;
    using Toybox.Graphics as Gfx;

    class ConstValues
    {
    const VALUE_0 = 0;
    const VALUE_1 = 0;
    const VALUE_2 = 0;
    const VALUE_3 = 0;
    const VALUE_4 = 0;
    const VALUE_5 = 0;
    const VALUE_6 = 0;
    const VALUE_7 = 0;
    const VALUE_8 = 0;
    const VALUE_9 = 0;
    }

    class StaticConstValues
    {
    static const VALUE_0 = 0;
    static const VALUE_1 = 0;
    static const VALUE_2 = 0;
    static const VALUE_3 = 0;
    static const VALUE_4 = 0;
    static const VALUE_5 = 0;
    static const VALUE_6 = 0;
    static const VALUE_7 = 0;
    static const VALUE_8 = 0;
    static const VALUE_9 = 0;
    }

    class StaticValues
    {
    static var VALUE_0 = 0;
    static var VALUE_1 = 0;
    static var VALUE_2 = 0;
    static var VALUE_3 = 0;
    static var VALUE_4 = 0;
    static var VALUE_5 = 0;
    static var VALUE_6 = 0;
    static var VALUE_7 = 0;
    static var VALUE_8 = 0;
    static var VALUE_9 = 0;
    }

    class Values
    {
    var VALUE_0 = 0;
    var VALUE_1 = 0;
    var VALUE_2 = 0;
    var VALUE_3 = 0;
    var VALUE_4 = 0;
    var VALUE_5 = 0;
    var VALUE_6 = 0;
    var VALUE_7 = 0;
    var VALUE_8 = 0;
    var VALUE_9 = 0;
    }

    class EnumValues
    {
    enum {
    VALUE_0,
    VALUE_1,
    VALUE_2,
    VALUE_3,
    VALUE_4,
    VALUE_5,
    VALUE_6,
    VALUE_7,
    VALUE_8,
    VALUE_9
    }
    }

    class StaticEnumValues
    {
    static enum {
    VALUE_0,
    VALUE_1,
    VALUE_2,
    VALUE_3,
    VALUE_4,
    VALUE_5,
    VALUE_6,
    VALUE_7,
    VALUE_8,
    VALUE_9
    }
    }

    function test(count, type, tname) {

    var array = new [count];

    var prev_free = Sys.getSystemStats().freeMemory;

    for (var i = 0; i < count; ++i) {
    array= new type();
    }

    var next_free = Sys.getSystemStats().freeMemory;

    Sys.println(Lang.format("$1$: $2$", [ (prev_free - next_free) / count, tname ]));
    }

    class MyApp extends App.AppBase
    {
    function initialize() {
    AppBase.initialize();
    }

    function onStart(state) {
    test(10, Values, "var");
    test(10, ConstValues, "const");
    test(10, EnumValues, "enum");
    test(10, StaticValues, "static var");
    test(10, StaticConstValues, "static const");
    test(10, StaticEnumValues, "static enum");
    }

    function getInitialView() {
    return [ new MyDataField() ];
    }

    function onStop(state) {
    }
    }

    class MyDataField extends Ui.DataField
    {
    function initialize() {
    DataField.initialize();
    }

    function onUpdate(dc) {
    var color = self.getBackgroundColor();
    dc.setColor(color, color);
    dc.clear();

    dc.setColor(~color & 0xFFFFFF, Gfx.COLOR_TRANSPARENT);
    }
    }
    [/code]

    The output indicates that static const and static var declarations don't affect the size of an object, but var, const and enum, and static enum do. I'm not sure I understand why enum and static enum values are instance variables (contribute to the overall size of each object instance). That said, I'm not sure I understand why non-static const variables are either.

    Travis
  • Thanks Travis, very detailed response.

    Having over the last 35 years worked on most languages, I quickly found enums don't seem to behave in the same way as other languages (C/C++/C#/Java) and tried to use them as described (as enumerating constants).

    I think I will use more classic techniques!

    Cheers
    Chris