PDA

View Full Version : How to run dynamically called functions


Jon-Huhn
05-06-2009, 02:43 PM
Without using the execute() command, I'm trying to build a function that will accept as arguments an object, the name of a function in that object, and any arguments needed for that function, and then run that function.

For example, the following snippet will do as described above (assuming no more than 3 arguments are given for the function):




fn run object funcName argArray = (local cachedFunc=getProperty object funcName

if argArray.count==0 then cachedFunc()
else if argArray.count==1 then cachedFunc argArray[1]
else if argArray.count==2 then cachedFunc argArray[1] argArray[2]
else if argArray.count==3 then cachedFunc argArray[1] argArray[2] argArray[3]

)



Now, my problem is that I need this to be able to support optional parameter keywords as well, and I can't figure out how to do it without breaking down and using exectute(), which is about 15 times slower (and therefore unacceptable for what I need).

Any ideas? Thanks!

MarcoBrunetta
05-06-2009, 03:34 PM
Well I'm not sure what you are trying to do, but if the optional keywords are inputted trough some sort of GUI control you should be able to access those controls (and the values) directly.

On a side note: it would be better to use CASE ergArray.count OF instead of having several different IF statements. Not sure about speed, but it's certainly "cleaner"

eek
05-06-2009, 03:48 PM
Without using the execute() command, I'm trying to build a function that will accept as arguments an object, the name of a function in that object, and any arguments needed for that function, and then run that function.

For example, the following snippet will do as described above (assuming no more than 3 arguments are given for the function):




fn run object funcName argArray = (local cachedFunc=getProperty object funcName

if argArray.count==0 then cachedFunc()
else if argArray.count==1 then cachedFunc argArray[1]
else if argArray.count==2 then cachedFunc argArray[1] argArray[2]
else if argArray.count==3 then cachedFunc argArray[1] argArray[2] argArray[3]

)



Now, my problem is that I need this to be able to support optional parameter keywords as well, and I can't figure out how to do it without breaking down and using exectute(), which is about 15 times slower (and therefore unacceptable for what I need).

Any ideas? Thanks!




This is not possible:

MyFunction parameter: (myOtherFunction parameter:10)
(
parameter()

)

without using a execute, yes is sucks and i want to be able to do it too.

Jon-Huhn
05-06-2009, 04:09 PM
Marco

You're right about the case command, I just did it this way in my posted code because I've alyways found if/then easier to read than case (believe it or not! :) )

As far as the GUI thing, no, that doesn't really pertain to what I'm doing. I'd try to explain it, but it would probably just confuse the issue more than it would clarify. Basically I just need to be able to do what I showed in the code snippet, only with the ability to use optional keywords as well (and not use execute() )

Thanks, though!

Jon-Huhn
05-06-2009, 04:11 PM
Yeah, definately suckage. Thanks, Charles.

CGTalk Moderation
05-06-2009, 04:11 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.