fpc/tests/test/toperator94.pp
svenbarth 66fa732720 Merged revision(s) 47634, 47655 from trunk:
* fix for Mantis : allow overloading of assignment operators that return ShortStrings with a specific size
+ 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... :/
........
* with the recent ShortString changes this test is no longer needed as it was added exactly to check the condition I removed, so disable it for 3.2.1 and newer (as I want to merge these changes back to fixes)
........

git-svn-id: branches/fixes_3_2@49055 -
2021-03-26 06:18:40 +00:00

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.