3

I have code like this:

TServer = class
private
    fOnMsgFromServer: TProc<String, String, String>;
public
    procedure RegisterOnMsgFromServer(aCallBack: TProc<String, String, String>);
    procedure Execute;
end;

procedure TServer.RegisterOnMsgFromServer(aCallBack: TProc<String, String, String>);
begin
    fOnMsgFromServer := aCallBack;
end;

procedure TServer.Execute;
begin
    fOnMsgFromServer('userName', 'password', 'message');
end;

Problem is in procedure Execute when I want put arguments to fOnMsgFromServer. "helper" shows me (Arg1: string; Arg2: string; Arg3: string), and i just don't know which parameter is which.

There is any solution to name this arguments?

aQuu
  • 565
  • 8
  • 18

1 Answers1

7

You cannot avoid these generic names if you use the generic TProc<T1,T2,T3> type. Its declaration looks like this:

type
  TProc<T1,T2,T3> = reference to procedure (Arg1: T1; Arg2: T2; Arg3: T3);

As you can see, this is where the names originate. If you use this type, you are stuck with these names.

Instead you should declare a bespoke reference procedure type rather than using the generic type. Not only will this let you impart meaningful names to the arguments, it also lets you stop repeating your self over and over.

type
  TMsgFromServerProc = reference to procedure(UserName, Password, Msg: string);

  TServer = class
  private
    fOnMsgFromServer: TMsgFromServerProc;
  public
    procedure RegisterOnMsgFromServer(aCallBack: TMsgFromServerProc);
  end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • So in case of Dictionary or List is impossible? dict: TDictionary ? – aQuu Nov 02 '16 at 17:04
  • 2
    Well, that's a type rather than a procedure. So the parameters are types rather than arguments. In any case, the types are given meaningful placeholder names. That type is `TDictionary`. And then methods take advantage of that. For instance, `procedure Add(const Key: TKey; const Value: TValue);` But in any case, that's a completely different matter than what you asked here. – David Heffernan Nov 02 '16 at 17:12