MX4*_*399 1 delphi generics interface delphi-xe
尝试构建基于接口和泛型的图并获得奇怪的错误 - 请注意错误行中"整数"一词的大小写差异.
文本解析器传递给Graph实现,然后由Graph调用以构建其基本数据结构.进一步的IGraphConstructor对象可以构建更复杂的实际图形,而不仅仅是填充基本字典.
IGraphConstructor<K,V> = interface
function Construct(AData : TObjectDictionary<K,V>) : boolean;
end;
IGraph<K,V> = interface
['{B25EEE1F-3C85-43BB-A56B-3E14F7EA926C}']
function Construct(AConstructor : IGraphConstructor<K,V>) : boolean;
function GetNodes : TObjectDictionary<K,V>;
property Nodes : TObjectDictionary<K,V> read GetNodes;
end;
TGraph<K,V> = class(TComponent, IGraph<K,V>)
private
FData : TObjectDictionary<K,V>;
function GetNodes : TObjectDictionary<K,V>;
...
//the editor
TVirtualEditor = class(TComponent)
private
FGlyphs : TGraph<integer,TGlyph>;
...
TTextParser<integer,TGlyph> = class(TInterfacedObject, IGraphConstructor<integer,TGlyph>)
...
Run Code Online (Sandbox Code Playgroud)
和...
function TVirtualEditor.Edit(AText: string): boolean;
var
parser : TTextParser<integer,TGlyph>;
begin
parser := TTextParser<integer,TGlyph>.Create(AText);
result := FGlyphs.Construct(parser);
end;
function TTextParser<integer,TGlyph>.Construct(AData: TObjectDictionary<integer,TGlyph>): boolean;
var
i : integer;
begin
for i := 1 to length(FText) do
begin
//#1
AData.AddOrSetValue(i, TGlyph(TCharGlyph.Create( FText[i] )) ); //!--> error [DCC Error] ...: E2010 Incompatible types: 'integer' and 'Integer'
end;
//uc....
end;
Run Code Online (Sandbox Code Playgroud)
将TTextParser声明为TTextParser<K,V>
并将其用作
TParser : TTextParser<integer,TGlyph>;
Run Code Online (Sandbox Code Playgroud)
在#1处的返回和错误
[DCC Error] ...: E2010 Incompatible types: 'K' and 'Integer'
Run Code Online (Sandbox Code Playgroud)
编辑:解决方法
找到了解决方法,但不确定是否可以这样做.
function TTextParser<K,V>.Construct(AData: TObjectDictionary<K,V>): boolean;
var
i : integer;
n : K;
o : V;
begin
for i := 1 to length(FText) do
begin
n := K((@i)^);
o := V(TCharGlyph.Create( FText[i] ));
AData.AddOrSetValue(n, o );
end;
result := true;
end;
Run Code Online (Sandbox Code Playgroud)
这条线
TTextParser<integer,TGlyph> = class(TInterfacedObject, IGraphConstructor<integer,TGlyph>)
Run Code Online (Sandbox Code Playgroud)
描述了一个泛型类型,其中两个使用的泛型类型名称是整数和TGlyph(如K和V一样IGraph<K,V>
).这些只是占位符,不应该与现有的类型混淆integer
和TGlyph
.
归档时间: |
|
查看次数: |
1639 次 |
最近记录: |