callback as Lang.Method() as Void as function parameter

Hello,

i recently started developing my first app for Garmin devices. I have an application where I want to change the interval of timers based on a state I get via REST API. So far so good.

I created my own Timer class including the Toybox.Timer and adding a getTime() Method. When I start a Timer, the time set will be stored in a privat variable I then can acces with this getter function.

This is the class:

class myTimer {
    hidden var _timer = new Timer.Timer();;
    hidden var _time;

    public function start(callback, time as Lang.Number, repeat as Lang.Boolean) as Void {
        _timer.start(callback, time , repeat);
        _time = time;
    }

    public function stop() as Void {
        _timer.stop();
    }

    public function getTime() as Lang.Number {
        return _time;
    }
}

Everything works fine. But theres one problem I encounter and find nothing about in the API docs or the Reference guide nor the sample projects. Looking at the start function:

public function start(callback, time as Lang.Number, repeat as Lang.Boolean) as Void {
    _timer.start(callback, time , repeat);
    _time = time;
}

I can tell the compiler I expect time to be a Lang.Number, and repeat a Lang.Boolean. but when I want callback to be a "callback as Lang.Method() as Void" i get a compile error. In the API docs its written exactly like this.

https://developer.garmin.com/connect-iq/api-docs/Toybox/Timer/Timer.html#start-instance_function

Error i get:

ERROR: instinct2: C:\Users\XXX\Documents\software\YYY\ZZZ\source\myTimer.mc:9,42: mismatched input '.' expecting {')', ','}
ERROR: instinct2: C:\Users\XXX\Documents\software\YYY\ZZZ\source\myTimer.mc:21: extraneous input '}' expecting {<EOF>, 'class', 'module', 'using', 'import', 'alias', 'typedef', 'public', 'const', 'native', 'var', 'enum', 'function', '('}

  • callback as Method() as Void works for me. I think you need to have import Toybox.Lang; in the same source file for this to work.

    Seems like a bug to me, especially given that the docs suggest callback as Lang.Method() as Void should work. Not the first time the docs have featured code that wouldn't compile, but this problem seems a bit more systemic (as it seems to affect any API call which receives a callback as a parameter.) I would open a bug report.

  • i have these included in the myTimer Class:

    using Toybox.Timer;
    using Toybox.Lang;
    using Toybox.System;
  • Then remove "Lang." from the callback parameter type and it will work.

    callback as Method() as Void

    The fact that callback as Lang.Method() doesn't work appears to be a bug in the language parser.

  • Now i got it working thanks!

    my findings if anyone stumbles across the same problem:

    does NOT work:

    using Toybox.Lang;

    public function start(callback as Method() as Void, time as Number, repeat as Boolean) as Void {
        _timer.start(callback, time , repeat);
        _time = time;
        _callback = callback;
    }

    does work, but callable has no type.

    using Toybox.Lang;

    public function start(callback, time as Lang.Number, repeat as Lang.Boolean) as Void {
        _timer.start(callback, time , repeat);
        _time = time;
        _callback = callback;
    }

    does NOT work:

    using Toybox.Lang;

    public function start(callback as Lang.Method() as Void, time as Lang.Number, repeat as Lang.Boolean) as Void {
        _timer.start(callback, time , repeat);
        _time = time;
        _callback = callback;
    }

    does NOT work:

    using Toybox.Lang as Lang;

    public function start(callback as Lang.Method() as Void, time as Lang.Number, repeat as Lang.Boolean) as Void {
        _timer.start(callback, time , repeat);
        _time = time;
        _callback = callback;
    }

    does work:

    import Toybox.Lang;

    public function start(callback as Method() as Void, time as Number, repeat as Boolean) as Void {
        _timer.start(callback, time , repeat);
        _time = time;
        _callback = callback;
    }

  • To be fair, all of those failed cases are expected except for "import and as Lang.Method()", since it's required to use "import Lang" (and not "using Lang") when omitting the module specifier (e.g. "Lang.") in a type declaration.

    In general, I think using is discouraged now.