Am 21.04.2021 um 20:05 schrieb Ryan Joseph via fpc-devel:

On Apr 20, 2021, at 11:38 PM, Sven Barth <pascaldra...@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
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to