E2506 在接口部分声明的参数化类型的方法不得使用局部符号
有人可以向我解释原因是什么,在创建泛型类时,我必须将私有常量移至接口部分吗?这正在扼杀我的设计,我不想让其他人看到应该私密的东西。
unit Unit38;
interface
uses
Generics.Collections;
type
TSimpleClass<T> = class(TObject)
private
procedure DoSomethingInternal(const SomeString: string);
public
procedure DoSomething;
end;
implementation
const
MyString = 'some string'; //Why this must be public?
{ TSimpleClass<T> }
procedure TSimpleClass<T>.DoSomething;
begin
DoSomethingInternal(MyString); //Compiler error
end;
procedure TSimpleClass<T>.DoSomethingInternal(const SomeString: string);
begin
//-------
end;
end.
谢谢。
could someone explain to me what is the reason that when creating a generic class I must move my private constans to the interface section? This is killing my design, I don't want others to see something that should be private.
unit Unit38;
interface
uses
Generics.Collections;
type
TSimpleClass<T> = class(TObject)
private
procedure DoSomethingInternal(const SomeString: string);
public
procedure DoSomething;
end;
implementation
const
MyString = 'some string'; //Why this must be public?
{ TSimpleClass<T> }
procedure TSimpleClass<T>.DoSomething;
begin
DoSomethingInternal(MyString); //Compiler error
end;
procedure TSimpleClass<T>.DoSomethingInternal(const SomeString: string);
begin
//-------
end;
end.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 Delphi 中泛型实现的结果。当您通过在另一个单元中提供具体 T 来实例化一个类时,该具体类的代码将被写入该另一个单元中。但是其他单元无法再看到您的私有字符串常量。这是相当令人沮丧的。
我对泛型实现的理解表明,Mikael 的解决方法将解决该问题,因为当您在另一个单元中实例化具体类型时,类 const 将是可见的。
It's a consequence of the generics implementation in Delphi. When you instantiate a class by supplying a concrete T in another unit, code for the concrete class is written into that other unit. But that other unit can no longer see your private string constant. It's rather frustrating.
My understanding of the generics implementation suggests that Mikael's workaround will solve the problem because the class const will be visible when you instantiate your concrete type in another unit.
D2010 中出现同样的错误,因此 D2010 的泛型修复并未解决此问题。这是一个错误: http://qc.embarcadero.com/wc/qcmain .aspx?d=79747
已在版本 15.0.3863.33207 中修复。我认为
这是 XE 另一个 QC 是: http://qc.embarcadero .com/wc/qcmain.aspx?d=78022,其中涉及枚举并且仍然处于打开状态。
顺便说一句,有关错误的文档不是很清楚。请参阅:
E2506 接口部分中声明的参数化类型的方法不得使用局部符号'%s'
涉及到泛型类中的类 var 不能在类的构造函数中分配文字(!)值,解决方法是对构造函数进行参数化...不知道为什么,但我想这与编译器限制有关。
Same error in D2010, so the generics fixes of D2010 did not address this. It is a bug: http://qc.embarcadero.com/wc/qcmain.aspx?d=79747
Fixed in build 15.0.3863.33207. Which I think is XE
Another QC on this is: http://qc.embarcadero.com/wc/qcmain.aspx?d=78022 which involves an enum and is still open.
The documentation on the error isn't very clear by the way. See:
E2506 Method of parameterized type declared in interface section must not use local symbol '%s'
It involves a class var in a generic class which cannot be assigned a literal (!) value in the class' constructor, the fix being to parameretrize the constructor... No idea why, but I guess it has to do with a compiler limitation.
这不是答案,但可能的解决方法是在类声明中使用 private const。
这适用于 Delphi 2010、XE 和 XE2,不适用于 Delphi 2009。
Not an answer but a possible workaround could be to use private const in the class declaration.
This works in Delphi 2010, XE and XE2, not in Delphi 2009.