Acknowledged

Toybox.Lang.Method documentation needs fixes

Toybox.Lang.Method has this example:

using Toybox.Timer;

var myCount = 0;

function timerCallback() {
    myCount += 1;
}

myTimer = new Timer.Timer();
myTimer.start(method(:timerCallback), 1000, true);

It's missing a 'var' on myTimer, and method is a method on Object, so its not clear what was intended here.

  • Ok... but its worse (I think). Even if you move the code into a function, and call that function, it *still* doesn't work, because 'method' only works when called from a class method, not a top level function (again, as far as I can tell), and it expects the name of a method defined on the object its called on. So you need a class containing both the definition of timerCallback, and a function wrapping the myTimer code.

    Maybe this is "normal" for the documentation and I've just not noticed. I noticed in this case, because I was looking for examples of how to use 'method' correctly... and unless I'm missing something, this example turned out to be about as incorrect as it could be. It seems to be worse than omitting it altogether.

    So I'd suggest either removing it, or providing something that does work - eg:

    import Toybox.Lang;
    import Toybox.Timer;
    class TimerTest {
        var myCount as Number = 0;
    
        function timerCallback() as Void {
            myCount += 1;
        }
    
        function setup() as Timer.Timer {
            var t = new Timer.Timer();
            t.start(method(:timerCallback), 1000, true);
            return t;
        }
    }
    

    To address your concern that the code wouldn't actually do anything (because the timer never gets setup), you could add:

    var myTimer as Timer.Timer = (new TimerTest()).setup();
    

    making it fully self contained.

  • If you look at our examples, very few of them are complete enough to compile and run after a simple copy/paste to global scope.

    We could add the var, but then the problem becomes that you can't call myTimer.start() at global scope like that. So then we add myFunction and move the myTimer code into that, but when you copy/paste that code into an app nothing ever happens.. because the code that creates the timer isn't ever called by anything.