类型X的参数必须支持接口Y. [英] Parameter of type X must support interface Y

查看:153
本文介绍了类型X的参数必须支持接口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 支持 IBar

如果你仔细想想,可以这样解释:

  TBar = TFoo< TBar> {TBar支持IBar?} 
|
TBar = TFoo< TBar> ... {ok,TBar支持IBar?}
|
TBar = TFoo< TBar> {ok,TBar支持IBar?}
|
{... turtles一路下来}

您要求编译器解决无限递归问题。它不能做到这一点。


I have a setup like so :

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;

This kind of structure allows me to build a sugar syntax like so :

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

The problem is that I get a compilation error, saying that

E2514 The type parameter TBuilder must support interface 'IBuilder'.

This is probably due to the typed constraint T: IBuilder present on the interface, even though TBuilder does support IBuilder (trough it's ancestor).

Can anyone please direct me on how to get around this?

Though, I cannot use TBuilder = class(TBuilder<IBuilder, TObject>)

解决方案

This can't be done. You're essentially trying to do this :

  IBar = interface(IInterface) end; 

  TFoo<T : IBar> = class(TObject, IBar) end;

  TBar = TFoo<TBar>;

Which generates error

E2086 Type 'TBar' is not yet completely defined

Without the interface dependence you can write this as

 TBar = class(TFoo<TBar>) end;

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 TBar support IBar?

If you think about it, this works out as :

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}

You're asking the compiler to solve an infinite recursion problem. It cannot do this.

这篇关于类型X的参数必须支持接口Y.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆