mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-22 21:32:02 +02:00

+ added tests The following rules for using these operator overloads as *implicit* overloads apply (Delphi compatible): - if a found assignment operator returns a default ShortString then that is used - if only one assignment operator to a String[x] is found then that is used - otherwise the assignment is not possible The explicit assignment checks for an exact match (and falls back for an implicit assignment). This is not entirely Delphi compatible as Delphi seems to favor the first found symbol in that case, but sometimes also not... :/ git-svn-id: trunk@47634 -
67 lines
1.3 KiB
ObjectPascal
67 lines
1.3 KiB
ObjectPascal
program toperator94;
|
|
|
|
{$mode objfpc}
|
|
{$modeswitch advancedrecords}
|
|
|
|
type
|
|
TString80 = String[80];
|
|
TString90 = String[90];
|
|
TString40 = String[40];
|
|
TString100 = String[100];
|
|
|
|
TTest1 = record
|
|
class operator :=(const aArg: TTest1): TString80;
|
|
end;
|
|
|
|
TTest2 = record
|
|
class operator :=(const aArg: TTest2): ShortString;
|
|
end;
|
|
|
|
var
|
|
ImplicitTest1ShortString: LongInt;
|
|
ImplicitTest1String80: LongInt;
|
|
ImplicitTest2ShortString: LongInt;
|
|
ImplicitTest2String80: LongInt;
|
|
|
|
class operator TTest1.:=(const aArg: TTest1): TString80;
|
|
begin
|
|
Writeln('TTest1 Implicit TString80');
|
|
Inc(ImplicitTest1String80);
|
|
Result := '';
|
|
end;
|
|
|
|
class operator TTest2.:=(const aArg: TTest2): ShortString;
|
|
begin
|
|
Writeln('TTest2 Implicit ShortString');
|
|
Inc(ImplicitTest2ShortString);
|
|
Result := '';
|
|
end;
|
|
|
|
operator :=(const aArg: TTest1): ShortString;
|
|
begin
|
|
Writeln('TTest1 Implicit ShortString');
|
|
Inc(ImplicitTest1ShortString);
|
|
Result := '';
|
|
end;
|
|
|
|
operator :=(const aArg: TTest2): TString80;
|
|
begin
|
|
Writeln('TTest2 Implicit TString80');
|
|
Inc(ImplicitTest2String80);
|
|
Result := '';
|
|
end;
|
|
|
|
var
|
|
t1: TTest1;
|
|
t2: TTest2;
|
|
s80: TString80;
|
|
begin
|
|
s80 := t1;
|
|
if ImplicitTest1ShortString <> 1 then
|
|
Halt(1);
|
|
s80 := t2;
|
|
if ImplicitTest2ShortString <> 1 then
|
|
Halt(2);
|
|
Writeln('ok');
|
|
end.
|