匿名方法作为函数结果
我想要做的是将作为函数结果获得的匿名方法分配给相同类型的变量。 Delphi 抱怨无法完成任务。显然,Delphi 我想分配“GetListener”函数而不是同一函数的结果。非常感谢对此的任何帮助。
type
TPropertyChangedListener = reference to procedure (Sender: TStimulus);
TMyClass = class
function GetListener:TPropertyChangedListener
end;
....
var MyClass: TMyClass;
Listener: TPropertyChangedListener;
begin
MyClass:= TMyClass.create;
Listener:= MyClass.GetListener; // Delphi compile error: E2010 Incompatible types: TPropertyChangedListener' and 'Procedure of object'
end;
What I want to do is to assign an anonymous method which I get as a function result to a variable of the same type. Delphi complains about not beeing able to do the assignement. Obviously Delphi things I want to assign the "GetListener" function instead of the result of that same function. Any help with this is very much appreciated.
type
TPropertyChangedListener = reference to procedure (Sender: TStimulus);
TMyClass = class
function GetListener:TPropertyChangedListener
end;
....
var MyClass: TMyClass;
Listener: TPropertyChangedListener;
begin
MyClass:= TMyClass.create;
Listener:= MyClass.GetListener; // Delphi compile error: E2010 Incompatible types: TPropertyChangedListener' and 'Procedure of object'
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用以下语法:
我编写了以下示例来明确 MyClass.GetListener() 和 MyClass.GetListener 分配之间的区别:
Use the following syntax:
I have written the following example to make clear the difference between the MyClass.GetListener() and MyClass.GetListener assignments:
我发现解决此问题的唯一方法是像这样声明 GetListener:
可能有一些语法可以强制编译器考虑函数的结果而不是函数本身,但我找不到任何语法。
The only way I've found to work around this is to declare your GetListener like this:
There might be some syntax to force the compiler to consider the function's result instead of the function itself, but I couldn't find any.