return a function from a different class using method(:)?

so, this is easiest to explain with code. but first: as to "why would you do that". i got a system where i can have an if loop once, and from then on just execute a variable containing the function which will gather information. this way i can add as many functions as i want wihtout compromising on performance. an external class (to reduce memory, it will be in a seperate class, only 30% of users will use this). needs to be able to acces those functions too, and while i am at it, i might just as well move all the functions to a seperate class, because i want this, no discussion possible.

now the introduction is done, here's a psuedo code example of what i want to do:

class datafactory {
    function createData() {
    
    }
    
    function createOtherData() {
    
    }

    // there are about 10-20 of these functions

}

class main extends WatchUi.WatchFace  {
    var factory = new datafactory()
    function getRequiredFunction (value) {
        if (value == 1) {
            return method(:factory.createData);
        } else if (value == 2) {
            return method(:factory.createOtherData);
        }
    
    
    }

}

that's roughly what i want to do. but when i do that, it says: could not find symbol "createData", or whatever the functionname is?

how should i do this?

  • Why not just return a method(:symbol) from datafactory?

  • (Because :symbol cannot have sub item.)

    (ETA And factory isn’t a symbol anyway.)

  • I'm going to try this, i think that'll be the solution, thanks

  • It seems that your code is pretty close to actually working. I think this should compile and run:

    class datafactory {
        function createData() {
        
        }
        
        function createOtherData() {
        
        }
    
        // there are about 10-20 of these functions
    
    }
    
    class main extends WatchUi.WatchFace  {
        var factory = new datafactory()
    
        function getRequiredFunction (value) {
            if (value == 1) {
                return factory.method(:createData);
            } else if (value == 2) {
                return factory.method(createOtherData);
            }
    
            return null;
        }
    }

    That said, this is a fairly inefficient (in terms of code space) to writing this...

    class datafactory {
        function createData() {
        }
        
        function createOtherData() {
        }
    
        // there are about 10-20 of these functions
    }
    
    class main extends WatchUi.WatchFace  {
        var factory = new datafactory()
    
        function getRequiredFunction(symbol) {
            if (factory has symbol) {
                return factory.method(symbol);
            }
            
            return null;
        }
    }

    This avoids the need for an external mapping from function to symbol, and then the if/else mapping to map back from number to function. Instead of this...

    enum {
      DATA_FUNCTION,
      OTHER_DATA_FUNCTION,
    }
    
    var f = view.getRequiredFunction(DATA_FUNCTION);
    
    // or without the enum, but not very readable
    var f = view.getRequiredFunction(1);

    you would just write this...

    var f = view.getRequiredFunction(:createData);

    Unfortunately, I still don't understand why you're doing it this way at all, but you are convinced that you want it this way so I'm not going to bother suggesting you do something simpler.