Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In Delphi, I could call functions dynamically using it's name like this:
Delphi
interface

type
  TQScrFuncType = function(Args: TQList): TQVar of Object;
  {$M+}
  TQScriptFunctions = class
  published
    function Add(Args: TQList): TQVar;
  public
    function Call(MName: String; Args: TQList): TQVar; Virtual;
  end;

implementation

function TQScriptFunctions.Add(Args: TQList): TQVar;
begin
  //Do something...
end;

function TQScriptFunctions.Call(MName: String; Args: TQList): TQVar;
var
  Md: TMethod;
begin
  Md.Code := Self.MethodAddress(MName);
  Md.Data := Pointer(Self);
  Result := TQScrFuncType(Md)(Args);
  Args.Free;
end;

The TQScriptFunctions.Call could call a function from it's name, I'm trying to port it to D, The D Language.
How do I do this in D, I have the name of a function in a string, and the arguments too, how do I call that function?

What I have tried:

I thought of putting the names and pointers to the functions in arrays, then search the array for that function name, and execute the function through it's pointer. It worked, but the problem is I have over 100 functions, that'll make things harder, and waste memory, and maybe slower (because it'll search for a string in a list of hundreds). Delphi is natively compiled, so is D, there should be a way to do this.
Posted
Comments
Sergey Alexandrovich Kryukov 28-Mar-16 2:16am    
You are right, Delphi is natively compiled, so the function/method names are, generally, stripped out of executable code.
Would would you want such feature.
Delphi has very powerful reflection, more powerful than that of .NET, but it works differently, without method names.
Generally, every technique based on some hard-coded names is flawed from the very beginning, bad for maintenance.
What do you need to achieve, ultimately?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900