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.
Defining it e.g. like a constant declaration:
const
AliasToUpperCase: function(const S: AnsiString): AnsiString = System.AnsiStrings.UpperCase;
might work for your needs.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With