Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2021-04

Re: How does TFPGMap key compare work?

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph
On Apr 20, 2021, at 11:38 PM, Sven Barth 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]?

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph
Ryan Joseph wrote 5 lines
> On Apr 20, 2021, at 11:38 PM, Sven Barth 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

Reply by email

Ryan Joseph
to Sven Barth
On Apr 21, 2021, at 11:21 PM, Sven Barth wrote: 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:

Then the compiler must do this for all strings types by default right?

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph

Ryan Joseph via fpc-devel schrieb am Fr.,
23. Apr. 2021, 00:53:

Show quoted text 11 lines
> On Apr 21, 2021, at 11:21 PM, Sven Barth wrote: > > 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: 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

Reply by email