匿名方法作为函数结果

发布于 2024-08-31 07:44:20 字数 587 浏览 5 评论 0原文

我想要做的是将作为函数结果获得的匿名方法分配给相同类型的变量。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混浊又暗下来 2024-09-07 07:44:20

使用以下语法:

  Listener:= MyClass.GetListener();

我编写了以下示例来明确 MyClass.GetListener() 和 MyClass.GetListener 分配之间的区别:

type
  TProcRef = reference to procedure(Sender: TObject);
  TFunc = function: TProcRef of object;

  TMyClass = class
    function GetListener: TProcRef;
  end;

function TMyClass.GetListener: TProcRef;
begin
  Result:= procedure(Sender: TObject)
  begin
    Sender.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  MyClass: TMyClass;
  ProcRef: TProcRef;
  Func: TFunc;

begin
  MyClass:= TMyClass.Create;
// standard syntax
  ProcRef:= MyClass.GetListener();

// also possible syntax
//  Func:= MyClass.GetListener;
//  ProcRef:= Func();

  ProcRef(MyClass);
end;

Use the following syntax:

  Listener:= MyClass.GetListener();

I have written the following example to make clear the difference between the MyClass.GetListener() and MyClass.GetListener assignments:

type
  TProcRef = reference to procedure(Sender: TObject);
  TFunc = function: TProcRef of object;

  TMyClass = class
    function GetListener: TProcRef;
  end;

function TMyClass.GetListener: TProcRef;
begin
  Result:= procedure(Sender: TObject)
  begin
    Sender.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  MyClass: TMyClass;
  ProcRef: TProcRef;
  Func: TFunc;

begin
  MyClass:= TMyClass.Create;
// standard syntax
  ProcRef:= MyClass.GetListener();

// also possible syntax
//  Func:= MyClass.GetListener;
//  ProcRef:= Func();

  ProcRef(MyClass);
end;
梦开始←不甜 2024-09-07 07:44:20

我发现解决此问题的唯一方法是像这样声明 GetListener:

procedure GetListener(var a: TPropertyChangedListener);

可能有一些语法可以强制编译器考虑函数的结果而不是函数本身,但我找不到任何语法。

The only way I've found to work around this is to declare your GetListener like this:

procedure GetListener(var a: TPropertyChangedListener);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文