Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an alias of a function/procedure?

In Delphi is there a way to declare a procedure as an alias of another? Something like:

function AnAliasToUpperCase(const S: AnsiString): AnsiString = system.AnsiStrings.UpperCase;  

and later in the program calling AnAliasToUpperCase or UpperCase must be exactly the same.

like image 203
zeus Avatar asked Jan 21 '18 20:01

zeus


2 Answers

Defining it e.g. like a constant declaration:

const
  AliasToUpperCase: function(const S: AnsiString): AnsiString = System.AnsiStrings.UpperCase;

might work for your needs.

like image 166
Victoria Avatar answered Nov 03 '22 00:11

Victoria


The proper answer to the question "How to make an alias to a function/procedure" is "You can't".

But there are two workarounds to simulate this which both might introduce a bit of overhead - the first is the const as shown in the other answer.

Additionally to declaring it as const you can also declare it as new inline routine:

function AliasToUpperCase(const S: AnsiString): AnsiString; inline;
begin
  Result := System.AnsiStrings.UpperCase(S);
end;

But then you are depending on the compiler settings for inlining and need to also add the AnsiStrings unit to wherever you are calling AliasToUpperCase or you will get the H2443 Inline function has not been expanded because unit is not specified in USES list warning.

For this function signature it works but for other return types you might suffer from missing return value optimization and have extra value copies.

like image 28
Stefan Glienke Avatar answered Nov 03 '22 00:11

Stefan Glienke