类型X的参数必须支持接口Y. [英] Parameter of type X must support interface Y
问题描述
IBuilder = interface(IInvokable)
end;
IBuilder< T:IBuilder; TOut:TWinControl> = interface(IInvokable)
end;
TBuilder< T:IBuilder; TOut:TWinControl> = class(TInterfacedObject,IBuilder,IBuilder< T,TOut>)
end;
TBuilder = class(TBuilder< TBuilder,TWinControl>)
end;
这种结构允许我创建一个糖语法,如下所示:
TBuilder< T:IBuilder; TOut:TWinControl> = class(TInterfacedObject,IBuilder,IBuilder< T,TOut>)
函数输出:TOut;
函数名称(aName:string):T;
函数Left(aLeft:Integer):T;
函数Top(aTop:Integer):T;
end;
$ b $ // ...稍后
TBuilder.Create()。Left(10).Top(5).Name('ABC'); //很好的一行
问题是我收到一个编译错误,说
E2514类型参数TBuilder必须支持'IBuilder'接口。
这可能是由于类型化约束 T:IBuilder
出现在界面上,即使TBuilder支持IBuilder(通过它的祖先)。
任何人都可以请我指导如何解决这个问题吗?
虽然我不能使用 TBuilder = class(TBuilder< IBuilder,TObject>)
这是无法完成的。你基本上是这样做的:
IBar = interface(IInterface)end;
TFoo< T:IBar> = class(TObject,IBar)end;
TBar = TFoo< TBar>;
其中产生错误
E2086类型'TBar'尚未完全定义
如果没有接口依赖性,您可以将其写为
TBar = class(TFoo< TBar>)end;
使其成为真正的后代,而不仅仅是一个别名。这通常可以解决该类型,但接口依赖性迫使编译器提出以下问题: TBar 支持 如果你仔细想想,可以这样解释: 您要求编译器解决无限递归问题。它不能做到这一点。 I have a setup like so : This kind of structure allows me to build a sugar syntax like so : The problem is that I get a compilation error, saying that This is probably due to the typed constraint Can anyone please direct me on how to get around this? Though, I cannot use This can't be done. You're essentially trying to do this : Which generates error E2086 Type 'TBar' is not yet completely defined Without the interface dependence you can write this as making it a true descendent and not just an alias. This could normally resolve the type, but the interface dependence is forcing the compiler to ask the question : Does If you think about it, this works out as : You're asking the compiler to solve an infinite recursion problem. It cannot do this. 这篇关于类型X的参数必须支持接口Y.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! IBar $ c
TBar = TFoo< TBar> {TBar支持IBar?}
|
TBar = TFoo< TBar> ... {ok,TBar支持IBar?}
|
TBar = TFoo< TBar> {ok,TBar支持IBar?}
|
{... turtles一路下来}
IBuilder = interface(IInvokable)
end;
IBuilder<T: IBuilder; TOut : TWinControl> = interface(IInvokable)
end;
TBuilder<T: IBuilder; TOut : TWinControl> = class(TInterfacedObject, IBuilder, IBuilder<T, TOut>)
end;
TBuilder = class(TBuilder<TBuilder, TWinControl>)
end;
TBuilder<T : IBuilder; TOut : TWinControl> = class(TInterfacedObject, IBuilder, IBuilder<T, TOut>)
function Output : TOut;
function Name(aName : string) : T;
function Left(aLeft : Integer) : T;
function Top(aTop : Integer) : T;
end;
// ... later
TBuilder.Create().Left(10).Top(5).Name('ABC'); // Nice one liner
E2514 The type parameter TBuilder must support interface 'IBuilder'.
T: IBuilder
present on the interface, even though TBuilder does support IBuilder (trough it's ancestor).TBuilder = class(TBuilder<IBuilder, TObject>)
IBar = interface(IInterface) end;
TFoo<T : IBar> = class(TObject, IBar) end;
TBar = TFoo<TBar>;
TBar = class(TFoo<TBar>) end;
TBar
support IBar
?TBar = TFoo<TBar> {TBar support IBar?}
|
TBar = TFoo<TBar>... {ok, TBar support IBar?}
|
TBar = TFoo<TBar> {ok, TBar support IBar?}
|
{...turtles all the way down}