I have problems to get generics to work in the following scenario:
Delphi provides the interface IComparable:
IComparable <T> = interface
function CompareTo (Value : T) : Integer;
end;
I add another interface IPersistent:
IPersistent = interface
function ToString : String;
procedure FromString (const Str : String);
end;
One example of a class implementing both interfaces:
TComparableString = class (TInterfacedObject, IComparable <String>, IPersistent)
strict private
FValue : String;
public
function CompareTo (Value : String) : Integer;
function ToString : String;
procedure FromString (const Str : String);
end;
Now for another generic class that has two interface constraints:
ISortIndex <VALUE_TYPE : IPersistent, IComparable> = interface
...
end;
And finally one implementation of that interface:
TSimpleSortIndex <VALUE_TYPE : IPersistent, IComparable> = class (TInterfacedObject, ISortIndex <VALUE_TYPE>)
...
end;
Now when I try to declare a sort index like that:
FSortIndex : ISortIndex <TComparableString>;
I get an error message
[DCC Error] Database.pas(172): E2514 Type parameter 'VALUE_TYPE' must support interface 'IComparable'
I tried several things but can’t get it to work.
Anyone in for some help? Thanks!
Your
TComparableStringclass doesn’t implement the non-genericIComparableinterface, so it doesn’t satisfy the type constraint. You’ll have to either change the constraint or implementIComparable.Changing the constraint is probably the easiest way forward. I don’t really know Delphi, but see if this works:
EDIT: I hadn’t noticed that your
TComparableStringimplementedIComparable<String>rather thanIComparable<TComparableString>. Is that deliberate? Usually something is comparable to other instances of itself, not to a different type.You could introduce another type parameter to
ISortIndex/TSimpleSortIndexto indicate the type thatVALUE_TYPEshould be comparable to – but I suspect it’s more sensible to changeTComparableString.