However you can only overload operators that are not already defined internally by the compiler. These operators are also defined when you declare a unique type alias (using type TypeName), because after all a unique type alias of e.g. LongInt is still a LongInt.
I respectfully disagree. On the Delphi page it is explicitly mentioned that using the type alias creates a new type that is not the same type:
... creates a new type called TMyInteger which is not identical to Integer.
However they are assignment compatible in certain cases as described.
Every type is compatible with itself. Two distinct types are compatible if they satisfy at least one of the following conditions.
Nowhere have I been able to read that in case types are compatible that the same operators are being used.
From your words I would have to conclude that as long as different types are "compatible" the same operators implementations are used for these "compatible" types.
Correct. After all you can still use
+ on a unique type alias of the
LongInt type. Also this compiles in Delphi:
program topovld;
{$APPTYPE CONSOLE}
type
TLongInt = type LongInt;
TTest = record
class operator Implicit(const aArg: LongInt): TTest;
class operator Implicit(const aArg: TTest): LongInt;
end;
class operator TTest.Implicit(const aArg: LongInt): TTest;
begin
Writeln('LongInt Implicit');
Result := Default(TTest);
end;
class operator TTest.Implicit(const aArg: TTest): LongInt;
begin
Writeln('TTest Implicit');
Result := 0;
end;
var
t: TTest;
l: LongInt;
tl: TLongInt;
begin
t := l;
l := t;
t := tl;
tl := t;
end.
As you can see the compiler will use the
LongInt assignment operators even for
TLongInt (it will however prefer a
TLongInt operator if one is declared).
Delphi also doesn't support global operator overloads only overloads where a record type is involved, so some things simply can't be tested.
That strikes me as odd (see quote below) but in case it is then so be it. Are you able to confirm or happen to know if that is mentioned somewhere in the Free Pascal documentation ?
Not that I'm aware of, but no one said that the documentation can't be improved... 🤷♀️
Edit: I rather wonder what type is better as a parameter in a procedure/function: string (=shortstring=255 bytes) or PChar (array defined earlier). My doubts are related to hardware limitations.
A
ShortString variable, especially if it's passed by-value will result in a 256-Byte push on the stack. For by-reference or result it will be a reference anyway.