Creating function pointers or delegates in ash?

cowlick

New member
In an ash script I want to call a function previously defined in the script. However, I have the name of the function in a string. Is there a way - possibly using call - to do this? A rough pseudo code example of my intent:

void funcA() {
print("funcA");
}
void funcB() {
print("funcB");
}

string toCall = "";

if (decision()) {
toCall = "funcA";
} else {
toCall = "funcB";
}

call( toCall );
 

Catch-22

Active member
In an ash script I want to call a function previously defined in the script. However, I have the name of the function in a string. Is there a way - possibly using call - to do this?

I don't know of a way to do that directly.. but you could do it pretty easily with a switch statement, which is probably how I would do it.

Code:
switch toCall {
    case "funcA": call(funcA); break;
    case "funcB": call(funcB); break;
}

Edit: If you enclose your pseudocode with [*code] and [*/code] it will be easier to read :)
 
Last edited:

Bale

Minion
Catch-22, I know what you're trying to do, but your syntax is rather screwed up, so that won't work. The way you're trying to do it, the call function is not what you're looking for. What you wanted to do this:

Code:
switch (toCall) {
    case "funcA": funcA(); break;
    case "funcB": funcB(); break;
}

Anyway, I don't think that is entirely what cowlick was looking for. The following will work. I only had to modify the line in red.

Code:
void funcA() {
   print("funcA");
}
void funcB() {
   print("funcB");
}

string toCall = "";

if (decision())  { 
    toCall = "funcA";
} else {
    toCall = "funcB";
}

[COLOR="Red"][B]call toCall();[/B][/COLOR]
I think that is exactly what you're hoping for. Am I wrong?
 
Last edited:
Top