How to use Method.initialize(aClass, aMethod)

I'm trying to use Method.initialize(aClass, aMethod) so I  can pass a function in my module as a parameter to another function.

With my module like this:

module myModule {
    function myFunction() as Void {
    }
}

I would do this:

var m = Method.initialize(myModule, :myFunction);

I get a runtime error that says "could not find symbol mClass"

I'm kind of assuming that a module can work as a Classdef because the documentation gives "Toybox.SensorHistory" as a Classdef example (which I believe is a module, not a class strictly speaking).  However, I also tried this just to test my assumption:

class myModule {
    public static function myFunction() as Void {
    }
}

And I get the same error.

Top Replies

All Replies

  • You don't want to actually call Method.Initialize(), but just method(),

    I can't think of any time you directly call initialize() in Monkey C.

    Adding a call back wfor a timer, for example:

    var timer= new Timer.Timer();
    timer.start( method(:onTimer), 1000, true );

    What is it you want to do?

  • I'll try this out tomorrow, but I was thinking modules don't have the method function because they don't extend Object.  In any case, they don't have an instance that could call the method function.  I'll be glad if I'm wrong though.

  • Again, I'm trying to figure out what you are trying to do.  Not the how (code), but the what.

    The main place I use modules is in barrels, and then have classes within that module,  And the classes usually have their own initialize, but I never call that initialize() directly.  It happens when I create an instance of the class.

    And I use method() in many apps in many different places.

  • You've got the gist of what I'm trying to do. Your ontimer example is about it (if it really makes a difference, I'm making a web request), but I'm trying to call it from a module, not a class function. My understanding is that the reason for Method.initialize(aClass, aMethod) is so you can create a callback that doesn't require an instance of an object. I just want to know what I'm misunderstand or doing wrong.

  • I do that all the time in two of my barrels that do makeWebrequest.  Using method()

    And no, I'm really not user what you are trying to do.  Is the function you are trying to get is within the module?  Are you trying to not use classes in a module?

  • My understanding is that the reason for Method.initialize(aClass, aMethod) is so you can create a callback that doesn't require an instance of an object.

    Yep.

    I just want to know what I'm misunderstand or doing wrong.

    As Jim explained, you don't directly call initialize() on any class - it's a constructor that's implicitly called by invoking new SomeClass. All you have to do is change Method.initialize to new Method.

    Even though the doc says "classdef", you can use the name of a module.

    For example, this works for me:

    module ExampleModule {
        function foo() {
            return 42;
        }
    }

    import Toybox.Activity;
    import Toybox.Lang;
    import Toybox.WatchUi;
    
    import ExampleModule;
    
    class testDatafieldView extends WatchUi.SimpleDataField {
        var moduleCallbackFn;
        
        function initialize() {
            SimpleDataField.initialize();
            label = "My Label";
            moduleCallbackFn = new Method(ExampleModule, :foo);
        }
    
        function compute(info as Activity.Info) as Numeric or Duration or String or Null {
            return moduleCallbackFn.invoke();
        }
    }

  • Of course!  Apparently for a while yesterday I just forgot  that "initialize" means that it's a constructor.  I feel a little stupid right now, but I am very grateful for your help.  This is exactly what what I was looking for.

  • No worries! Also note that $ is the "global module", so you can use that to make a callback from a function that is truly global (exists neither at the class nor module level). (You can also use that to call global functions slightly more efficiently.)