We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › call function knowing its name in string form
Page Index Toggle Pages: 1
call function knowing its name in string form (Read 961 times)
call function knowing its name in string form
Mar 12th, 2009, 7:24pm
 
hello. I used to code in actionscript, where it is possible to call a funnction like:
String n = "myFunction";
this[n]();

void myFunction () {
...

is there an equivalent in processing? Or maybe a different aproach?
Re: call function knowing its name in string form
Reply #1 - Mar 12th, 2009, 8:25pm
 
look at java's apply function. I believe it's something like:
callingObject.apply("function name", [array of parameters])

The only thing is that you have to write your functions to take an array of parameters if you want it to take more than one I believe.
Re: call function knowing its name in string form
Reply #2 - Mar 12th, 2009, 9:58pm
 
If you're wanting to use the function as a string, you're probably doing it wrong.

It is possible, but in the course of most normal projects, you really shouldn't need to do such a thing.
Re: call function knowing its name in string form
Reply #3 - Mar 12th, 2009, 10:55pm
 
Skew wrote on Mar 12th, 2009, 7:24pm:
Or maybe a different aproach

Yes.
But explaining what you are trying to do might help us in showing how to do it... Smiley
Re: call function knowing its name in string form
Reply #4 - Mar 12th, 2009, 11:28pm
 
i couldnt find a reference for this function on the net. the closest was http://java.sun.com/j2se/1.5.0/docs/api/javax/management/QueryExp.html#apply(javax.management.ObjectName) but this doesnt seem to bet the function you are talking about. Processing says the function does not exist, same in java mode...


I am making some cpu intensive recursive calculations, thats why i need to spend only a specified amount of time every draw cycle making these calculations, so it wouldnt hang up. So i have an idea to store functions in a queue (array of string function names), and a hashmap to store these functions' arguments, although i dont really know how to implement the hashmap either...
this way i think i could simply call:
addToQueue("funcName",[args]);

sorry for my crude knowledge of common practices and design patterns..
Re: call function knowing its name in string form
Reply #5 - Mar 12th, 2009, 11:45pm
 
If that's all you need, use a switch() statement.  Your idea about queueing up functions by name in an arraylist or whatever is pretty interesting, and you're right, you'll probably have to store the arguments (if they're dynamic) in a hashmap.  But to call them, you hardly need to resort to weird trickery.

Let's assume you've already gotten the name out of your queue and the argument list out of the hashmap.  You seem ok on that part, so I won't go into it.  Then:

String funcName = ...; // you figure out this part
object[] funcArgs = ...;

switch(funcName) {
 case "myFunc1": // takes 2 args, an int and a float
   myFunc1((int)(funcArgs[0]), (float)(funcArgs[1]));
   break;
 case "myFunc2:" // takes no arguments.
   myFunc2();
   break;
 default:
   break;
}

You don't have to do any weird dynamic marshalling of a string to a function object.  What you will have to do is coerce the function arguments to the proper types at the point of the call, although if you know that all the functions always take the same argument types, and it's the same type (e.g. they all take 3 float values or whatever), then you can declare funcArgs as an array of the right type to begin with and avoid all those casts at the point of the call.  But the above method, where the args are stored as arrays of generic objects, will work for any argument list regardless of types.
Re: call function knowing its name in string form
Reply #6 - Mar 13th, 2009, 3:04am
 
I have thought about this, but i tried to achieve as much universality as possible. This way its quite deterministic, tightly coupled. But i guess its a more realistic option.
Except that it fails when i have function with same name, but different arguments. I checked into recursive switching but it seems that processing only takes constant expressions, no variables, as cases :/
That increases the number of switch cases to functionNames*argumentNumbers*argumentTypes... there must be an easier way... I thought about storing types in a hashmap but to cast a type knowing its string name sounds even more absurd. ( i mean myFunc( (hashmap.get("argumentsType") ) argumentsArray[x] ) )
ok more research needed...
Re: call function knowing its name in string form
Reply #7 - Mar 13th, 2009, 4:01pm
 
You'll make your life an awful lot easier if you look up multi-threading, have all your calculations going on in a separate thread, and drawing back in the main thread. You can set the calculations thread to be a lower priority so the main draw thread can reliably interrupt it every so often to draw the current state.
Re: call function knowing its name in string form
Reply #8 - Mar 15th, 2009, 1:44am
 
thanks.

i am looking into multithreading. Interesting stuff.
also i found the java.lang.reflect package.
now i can acomplish my task.
also im thinking of writing a library for processing, time will tell...
Page Index Toggle Pages: 1