Sign in Join

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

Mailing lists / fpc-pascal / 2017-09

Comparison operator for generics

7 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Ryan Joseph

This is a common pattern I’m finding when working with generics in FPC. In the example below there is an equals comparison that needs to be checked but for some types such as records there is no default operator overload so I need to add one for every record that will be specialized in the class.

Is there a way around this? I’d like to just skip the comparison for specialized types that can’t be compared if possible.

type
	generic TCollection<T> = class
		procedure AddValue (value: T);
	end;
procedure TCollection.AddValue (value: T);
begin
	// comparison operator doesn't exist for records so I get compiler errors if I specialize TCollection with a record type
	if value = T then
		;
end;

Regards,
Ryan Joseph

Sven Barth
to Ryan Joseph
"Ryan Joseph" wrote
This is a common pattern I’m finding when working with generics in FPC.

In the example below there is an equals comparison that needs to be checked but for some types such as records there is no default operator overload so I need to add one for every record that will be specialized in the class.

Is there a way around this? I’d like to just skip the comparison for

specialized types that can’t be compared if possible.

Show quoted text 10 lines
type generic TCollection<T> = class procedure AddValue (value: T); end; procedure TCollection.AddValue (value: T); begin // comparison operator doesn't exist for records so I get

compiler errors if I specialize TCollection with a record type

if value = T then ; end;

You could try to use the TEqualityComparer (or so) types from the rtl-generics package (don't know in which unit exactly they are ^^') as those can work with records without operator overloads.

Regards,
Sven

Ryan Joseph
to Sven Barth
On Sep 12, 2017, at 12:59 PM, Sven Barth via fpc-pascal wrote: You could try to use the TEqualityComparer (or so) types from the rtl-generics package (don't know in which unit exactly they are ^^') as those can work with records without operator overloads.

Do any examples of how to use the class exist? I searched around and couldn’t find a single snippet even though it appears to exist in Delphi.

Regards,
Ryan Joseph

Ryan Joseph
to Maciej Izak
On Sep 12, 2017, at 4:46 PM, Maciej Izak wrote: Please check example at : packages\rtl-generics\examples\tcomparer\tcomparerproject.lpr
Thanks. What is the & here? TCompare.&String(Left.Name, Right.Name);

I don’t think I’ve ever seen that before.

Regards,
Ryan Joseph

Michael Van Canneyt
to Ryan Joseph
Ryan Joseph wrote 8 lines
> On Sep 12, 2017, at 4:46 PM, Maciej Izak wrote: > > Please check example at : packages\rtl-generics\examples\tcomparer\tcomparerproject.lpr Thanks. What is the & here? TCompare.&String(Left.Name, Right.Name); I don’t think I’ve ever seen that before.

It means you use a language keyword as an identifier. An ugly habit, but unfortunately inherited from Delphi.

Originally intended to be able to create imports of other languages (think SOAP, COM) it is now of course abused...

Michael.