[fpc-devel] [fpc-pascal] How does TFPGMap key compare work?

Sven Barth pascaldragon at googlemail.com
Thu Apr 22 07:21:52 CEST 2021


Am 21.04.2021 um 20:05 schrieb Ryan Joseph via fpc-devel:
>
>> On Apr 20, 2021, at 11:38 PM, Sven Barth <pascaldragon at googlemail.com> wrote:
>>
>> All four string types provide built in > and < operators:
> On a side note how do you even make overloads (or type helpers for that matter) for short strings because String[10] isn't the same type as String[100]?

You need to use named types, though for operators this is less useful, 
because the compiler will implicitly convert different ShortString types 
to find a suitable operator overload:

=== code begin ===

{$mode objfpc}

type
   TString20 = String[20];
   TString40 = String[40];
   TString60 = String[60];

operator >< (aLeft: TString60; aRight: TString40): TString20;
begin
   Result := 'foo';
end;

var
   ss10: String[10];
   ss20: TString20;
   ss40: TString40;
begin
    ss10 := ss40 >< ss20;
end.

=== code end ===

For type helpers the compiler is stricter and does not allow the usage 
on unnamed variables in that case (this is currently first and foremost 
a technical restriction due to how the lookup is done):

=== code begin ===

{$mode objfpc}

type
   TString20 = String[20];
   TString40 = String[40];
   TString60 = String[60];

   TString40Helper = type helper for TString40
     procedure Test;
   end;

procedure TString40Helper.Test;
begin

end;

var
   ss40: TString40;
   ss40_2: String[40];
   ss60: TString60;
begin
   ss40.Test; // ok
   ss40_2.Test; // not ok
   ss60.Test; // not ok
end.

=== code end ===

Regards,
Sven


More information about the fpc-devel mailing list