Unable to assign const array at module level. Ok at class level.

Former Member
Former Member
As the title states, I am unable to assign const array at module level, but I can at class level. It is a small array of only 3 integers.

Failed invoking <symbol>
UnexpectedTypeException: Expected Object, given Method
<init> in ......mc:11
onStart in ......mc:15
Unhandled Exception
Failed invoking <symbol>
Socket Error in packet header 0
  • You might need to post some code. It works fine for me. All I've done is generated a new project (File > New ConnectIQ Project > ...) and then updated the generated ZZZApp.mc to look like this...

    using Toybox.Application as App;
    using Toybox.Math as Math;
    using Toybox.Graphics as Gfx;

    module X {

    const x = [ Gfx.FONT_SMALL, Math.PI, 4 ];

    }

    const y = [ 3, 2, 1 ];

    class ZZZApp extends App.AppBase {

    using Toybox.System as Sys;

    function initialize() {
    AppBase.initialize();
    }

    //! onStart() is called on application start up
    function onStart() {
    Sys.println(X.x);
    Sys.println(y);
    }

    //! onStop() is called when your application is exiting
    function onStop() {
    }

    //! Return the initial view of your application here
    function getInitialView() {
    return [ new ZZZView() ];
    }

    }


    I get no compile warning or error.

    That said, I did have problems if I put the using clause for Math and Gfx inside the module declaration. I think that is probably a different bug.
  • Former Member
    Former Member over 9 years ago
    I have worked out that the problem appears to be specific to Scoping with using. Uncomment "using DemoMod as DemoMod;" to see the problem.
    using Toybox.Application as App;
    using Toybox.Graphics as Gfx;
    using Toybox.WatchUi as Ui;
    //using DemoMod as DemoMod;


    class errorApp extends App.AppBase {


    var modClass;


    function initialize() {
    AppBase.initialize();
    }


    //! onStart() is called on application start up
    function onStart() {
    System.println(DemoMod.A);
    }


    function getInitialView() {
    return [ new errorView(), new errorDelegate() ];
    }


    }


    class errorView extends Ui.View {


    function onShow(dc) {
    requestUpdate();
    }


    function onUpdate(dc) {
    dc.setColor(Gfx.COLOR_BLACK, Gfx.COLOR_WHITE);
    dc.clear();
    dc.drawText(dc.getWidth()/2, dc.getHeight()/2, Gfx.FONT_MEDIUM, "View", Gfx.TEXT_JUSTIFY_CENTER|Gfx.TEXT_JUSTIFY_VCENTER);
    }
    }


    class errorDelegate extends Ui.BehaviorDelegate {


    function initialize() {
    BehaviorDelegate.initialize();
    }


    function onBack() {
    Ui.popView(Ui.SLIDE_RIGHT);
    return true;
    }


    }


    module DemoMod {


    const A = [0, 1, 2];
    }




  • Former Member
    Former Member over 9 years ago
    To further complicate matters I have been able to use it in the declaration of a View. Perhaps this problem is specific to the AppBase declaration page?