[fpc-pascal] Comparison operator for generics

Sven Barth pascaldragon at googlemail.com
Tue Sep 12 07:59:34 CEST 2017

Am 12.09.2017 06:49 schrieb "Ryan Joseph" <ryan at thealchemistguild.com>:
> 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;

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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170912/0a6da08c/attachment.html>

More information about the fpc-pascal mailing list