How to make a function / procedure alias?

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

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

and then the program calling AnAliastoUpperCase or UpperCase should be exactly the same.

+7
function alias delphi
source share
2 answers

Defining it, for example. as a constant declaration:

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

can work for your needs.

+23
source share

The correct answer to the question "How to create an alias for a function / procedure" is "You cannot."

But to simulate, there are two workarounds that can add a bit of overhead - the first is a constant, as shown in another answer.

In addition to declaring it as const, you can also declare it as a new built-in procedure:

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

But then you depend on the compiler settings for inline, and you also need to add the AnsiStrings block where you call AliasToUpperCase , or you will get a warning H2443 Inline function has not been expanded because unit is not specified in USES list .

It works for this function signature, but for other types of returned data, you may encounter a lack of optimization of the return value and have copies of additional values.

+4
source share

All Articles