[fpc-pascal] Comparison operator for generics
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.
> generic TCollection<T> = class
> procedure AddValue (value: T);
> procedure TCollection.AddValue (value: T);
> // comparison operator doesn't exist for records so I get
compiler errors if I specialize TCollection<> with a record type
> if value = T then
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...
More information about the fpc-pascal