Class: Toybox.Lang.Method

Inherits:
Toybox.Lang.Object show all

Overview

Method is a class that represents a callback, or a function that can be used as an argument to another function. You can create one using the method() call, and invoke the Method using the invoke() method.

See Also:

Example:

Using a callback function with a Timer

using Toybox.Timer;

var myCount = 0;

function timerCallback() {
    myCount += 1;
}

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

Example:

Invoking a Method

using Toybox.Lang;

function sensorIterator(type, options) {
    var sensors = [
        :getHeartRateHistory,
        :getTemperatureHistory,
        :getPressureHistory,
        :getElevationHistory
    ];

    var getSensorHistory = new Lang.Method(Toybox.SensorHistory, sensors[type]);
    return getSensorHistory.invoke(options);
}

enum {
    HEARTRATE,
    TEMPERATURE,
    PRESSURE,
    ELEVATION
}

var elevationIter = sensorIterator(ELEVATION, {:period => 10 });

Since:

API Level 1.0.0

Instance Method Summary collapse

Instance Method Details

hashCode() as Lang.Number

Get a hash code value for a Method. This computes a 32-bit Number that is typically used as an index when placing Objects into a Dictionary. Hash code values have the following characteristics:

  • The computed hash code is constant for the lifetime of an Object

  • If two Objects are equal, their hash codes will be equal

Returns:

See Also:

Since:

API Level 1.0.0

initialize(aClass, aMethod)

Method Constructor.

Parameters:

  • aClass

    Classdef of method (e.g. Toybox.SensorHistory)

  • aMethod(Lang.Symbol)

    Symbol of class object

Since:

API Level 1.0.0

invoke(parameters...) Lang.Object

Invoke a Method.

Parameters:

  • parameters...(Lang.Object)

    The parameters required by the invoked Method

Returns:

  • Lang.Object

    The return value from the invoked Method

Since:

API Level 1.0.0


Generated Nov 29, 2021 1:34:35 PM