Post Reply 
question : a trick to call function from a variable
10-08-2016, 10:12 PM
Post: #1
question : a trick to call function from a variable
hello,
I have a function name in a variable, and I would like to call it, some one know how to do ?
example :
Code:

EXPORT myfct(x)
BEGIN
  PRINT(x);
END;

EXPORT test(fct)
BEGIN
  local call:=fct+"(123)";
  EVAL(call);
END;
Now, If I call test("myfct"), Iwant the function myfct to be called (with parameter 123)
But it does not works as I wanted :
it returns "myfct(123)" but does not execute it...
if someone can help me..
Thank you.

primer
Find all posts by this user
Quote this message in a reply
10-08-2016, 11:09 PM
Post: #2
RE: question : a trick to call function from a variable
I think
EVAL(EXPR(call))
Or perhaps just
EXPR(call)
Will do what you want.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
10-09-2016, 11:14 AM
Post: #3
RE: question : a trick to call function from a variable
Hi StephenG1CMZ,
Thank you very much, it's OK.

By the way, a note for thoses who may want to use such callback trick : if you want to use string as function parameters, you have to protect them with backslashs (\").

For example par2 accept string abcd

Code:
EXPORT myfct(par1, par2)
BEGIN
  PRINT(par1+" and "+par2);
END;

EXPORT test(fct)
BEGIN
  local call:=fct+"(123,\"abcd\")";
  EVAL(call);
END;

primer
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)