Discussion:
[fpc-devel] [fpc-pascal] How does TFPGMap key compare work?
Ryan Joseph via fpc-devel
2021-04-21 18:05:11 UTC
Permalink
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]?

Regards,
Ryan Joseph

_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cg
Sven Barth via fpc-devel
2021-04-22 05:21:52 UTC
Permalink
Post by Ryan Joseph via fpc-devel
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-***@lists.freepascal.org
https://lists.fr
Ryan Joseph via fpc-devel
2021-04-22 20:26:36 UTC
Permalink
Then the compiler must do this for all strings types by default right?

Regards,
Ryan Joseph

_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/
Sven Barth via fpc-devel
2021-04-23 05:53:31 UTC
Permalink
Post by Sven Barth via fpc-devel
You need to use named types, though for operators this is less useful,
because the compiler will implicitly convert different ShortString types to
Then the compiler must do this for all strings types by default right?
For the compiler it's more often than not simply a stringdef with the
string type set to st_shortstring. The length of that stringdef comes into
play much less often.

Regards,
Sven
Loading...