Array Element Type Confusion

Arrays, as you know, can contain elements of different types. This works perfectly fine:

var arr1 = new[10];

arr1 = [2, "string"; false, 3.14156];

Arrays can also contain "resource" elements. This also works fine.

var arr2 = new[10];

arr2 = [ WatchUi.loadResource(Rez.Fonts.myML), WatchUi.loadResource(Rez.Fonts.myXL) ]

=====================

What would be really nice is to create an array that contains System Fonts (integers) and custom fonts (resources). Then you could iterate thru you set of fonts to pick the best one. Like this:

fontArray = [ Gfx.FONT_SMALL, Gfx.FONT_MEDIUM, WatchUi.loadResource(Rez.Fonts.myML), Gfx.FONT_LARGE ];

======================

But, at least from what I'm seeing, arrays can contain resources, or any other combination of data types. But not resources AND any other data type. So the above causes an error. Ugh.

Is this an intentional limitation, or a parser bug that triggers an error?

  • What's the error and where are you trying  to initialize the array?  I have the following in initialize() and no problem when compiling or running:

    largeF=Ui.loadResource(Rez.Fonts.large);
            mediumF=Ui.loadResource(Rez.Fonts.medium);
            smallF=Ui.loadResource(Rez.Fonts.small);
            iconF=Ui.loadResource(Rez.Fonts.icon);
            cbF=Ui.loadResource(Rez.Fonts.cb);
            var a=[largeF,mediumF,smallF,iconF,cbF,Gfx.FONT_SMALL];

    And if I have:

    var a=[Ui.loadResource(Rez.Fonts.large),mediumF,smallF,iconF,cbF,Gfx.FONT_SMALL];  

    again, no problem.

  • Thanks Jim - sometimes I just have to laugh at stupid working too fast logic errors that then waste too much time trying to isolate and debug. In this case, for some reason, I made the absurd leap that (for example), the array's value at index 1 dynamically updated to 7 later on. And then when created a test array with both data types and did a System.print, I got an error... because System.print errors out when you try to print a "resource". Too bad (like null) System.print doesn't print out something like <resource>. Anyway, all good now.

    var var1 = 2;
    var arr1 = [1, var1, 3, 4];
    var1 = 7;