Documentation clarification: Object.method() and Lang::Method

Former Member
Former Member
Just thought I would clarify this for others:

When you call method( :some-symbol) in an object instance method, that call is effectively equivalent to "new Lang.Method(self, :some-symbol)"

The docs for Method.initalize() say that initialize expects "Classdef of method" as the first parameter; however, the initialize function really requires an object as it's first parameter. (self is a reserved word in MonkeyC that refers to the current instance of the class that the currently executing method is a member of - roughly equivalent to 'this' in C++.) Note that you can create a method object that will invoke some method on some other object by using "new Lang.Method(some_other_object, :some-symbol)". Unfortunately, there doesn't appear to be any equivalent syntax / construct to refer to instance variables - but that can obviously be implemented by creating member functions for the variables you need to expose (wrap a system object with a new class if necessary).

Unfortunately, the docs give no indication if the Method object obtains a strong or weak reference on the target object instance.

As with many places in MonkeyC (but not all), :some-symbol can be a variable that holds an instance of Symbol. (And again it's really unfortunate that Symbol.toString() always returns the string 'Symbol': quite useless.) A notable place that doesn't accept a variable holding a Symbol object is the rhs of the 'has' operator, which is also really unfortunate - especially given that there is no way to trap a "symbol not found" error.

(The above all applies to SDK 1.2.2; not sure about any other version.)

-frank
  • however, the initialize function really requires an object as it's first parameter.

    Note that you can also invoke a module function like this... new Lang.Method(Toybox.Activity, :getActivityInfo)[/code], which I don't believe you can do with the method() method..

    Note that you can create a method object that will invoke some method on some other object by using "new Lang.Method(some_other_object, :some-symbol)".

    You can more simply write some_other_object.method(:some_symbol) which is much more readable.

    Unfortunately, there doesn't appear to be any equivalent syntax / construct to refer to instance variables - but that can obviously be implemented by creating member functions for the variables you need to expose (wrap a system object with a new class if necessary).

    Other dynamically typed languages allow this, and I'm fairly certain it works with MonkeyC. I don't have a compiler at work, but...

    class X {
    var a;
    var b;
    }

    var x = new X;
    x[:a] = 1;


    Unfortunately, the docs give no indication if the Method object obtains a strong or weak reference on the target object instance.

    It is a strong reference. If it were a weak reference, then it would be possible for the referenced object to be destroyed before invoking the callback, and the callback couldn't be invoked.

    As with many places in MonkeyC (but not all), :some-symbol can be a variable that holds an instance of Symbol. A notable place that doesn't accept a variable holding a Symbol object is the rhs of the 'has' operator, which is also really unfortunate.

    The following runs fine for me. It prints true twice (I've pasted it into the initialize() method of a class derived from Ui.View).

    var x = (self has :onUpdate);
    Sys.println(x);

    var symbol = :onUpdate;
    var y = (self has symbol);
    Sys.println(y);


    Travis