【发布时间】:2009-07-30 09:37:35
【问题描述】:
我无法让泛型在以下情况下工作:
Delphi提供了IComparable接口:
IComparable <T> = interface
function CompareTo (Value : T) : Integer;
end;
我添加另一个接口IPersistent:
IPersistent = interface
function ToString : String;
procedure FromString (const Str : String);
end;
实现两个接口的类的一个示例:
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;
现在是另一个具有两个接口约束的泛型类:
ISortIndex <VALUE_TYPE : IPersistent, IComparable> = interface
...
end;
最后是该接口的一个实现:
TSimpleSortIndex <VALUE_TYPE : IPersistent, IComparable> = class (TInterfacedObject, ISortIndex <VALUE_TYPE>)
...
end;
现在,当我尝试声明这样的排序索引时:
FSortIndex : ISortIndex <TComparableString>;
我收到一条错误消息
[DCC Error] Database.pas(172): E2514 Type parameter 'VALUE_TYPE' must support interface 'IComparable'
我尝试了几件事,但无法正常工作。
有人帮忙吗?谢谢!
【问题讨论】:
标签: delphi generics interface delphi-2009