Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Get the Name of the Current Procedure/Function in Delphi (As a String)

Is it possible to obtain the name of the current procedure/function as a string, within a procedure/function? I suppose there would be some "macro" that is expanded at compile-time.

My scenario is this: I have a lot of procedures that are given a record and they all need to start by checking the validity of the record, and so they pass the record to a "validator procedure". The validator procedure (the same one for all procedures) raises an exception if the record is invalid, and I want the message of the exception to include not the name of the validator procedure, but the name of the function/procedure that called the validator procedure (naturally).

That is, I have

procedure ValidateStruct(const Struct: TMyStruct; const Sender: string); begin  if <StructIsInvalid> then     raise Exception.Create(Sender + ': Structure is invalid.'); end; 

and then

procedure SomeProc1(const Struct: TMyStruct); begin   ValidateStruct(Struct, 'SomeProc1');   ... end;  ...  procedure SomeProcN(const Struct: TMyStruct); begin   ValidateStruct(Struct, 'SomeProcN');   ... end; 

It would be somewhat less error-prone if I instead could write something like

procedure SomeProc1(const Struct: TMyStruct); begin   ValidateStruct(Struct, {$PROCNAME});   ... end;  ...  procedure SomeProcN(const Struct: TMyStruct); begin   ValidateStruct(Struct, {$PROCNAME});   ... end; 

and then each time the compiler encounters a {$PROCNAME}, it simply replaces the "macro" with the name of the current function/procedure as a string literal.

Update

The problem with the first approach is that it is error-prone. For instance, it happens easily that you get it wrong due to copy-paste:

  procedure SomeProc3(const Struct: TMyStruct);   begin     ValidateStruct(Struct, 'SomeProc1');     ...   end; 

or typos:

procedure SomeProc3(const Struct: TMyStruct); begin   ValidateStruct(Struct, 'SoemProc3');   ... end; 

or just temporary confusion:

procedure SomeProc3(const Struct: TMyStruct); begin   ValidateStruct(Struct, 'SameProc3');   ... end; 
like image 730
Andreas Rejbrand Avatar asked May 12 '10 09:05

Andreas Rejbrand


People also ask

How do you call a function in Delphi?

You can make the call using the declared name of the routine (with or without qualifiers) or using a procedural variable that points to the routine. In either case, if the routine is declared with parameters, your call to it must pass parameters that correspond in order and type to the parameter list of the routine.

What is procedure in Delphi?

In Delphi, there are generally two types of subroutines: a ​function and a procedure. The usual difference between a function and a procedure is that a function can return a value, and a procedure generally will not do so. A function is normally called as a part of an expression.


1 Answers

We are doing something similar and only rely on a convention: putting a const SMethodName holding the function name at the very beginning.
Then all our routines follow the same template, and we use this const in Assert and other Exception raising.
Because of the proximity of the const with the routine name, there is little chance a typo or any discrepancy would stay there for long.
YMMV of course...

procedure SomeProc1(const Struct: TMyStruct); const   SMethodName = 'SomeProc1'; begin   ValidateStruct(Struct, SMethodName);   ... end;  ...  procedure SomeProcN(const Struct: TMyStruct); const   SMethodName = 'SomeProcN'; begin   ValidateStruct(Struct, SMethodName);   ... end; 
like image 168
Francesca Avatar answered Sep 26 '22 02:09

Francesca