Can I use a closure on an event handler (ie, TButton OnClick)

前端 未结 4 1996
迷失自我
迷失自我 2020-12-24 03:44

If I try to use a closure on an event handler the compiler complains with :

Incompatible types: \"method pointer and regular procedure\"

which I understand..

相关标签:
4条回答
  • 2020-12-24 04:05

    An excellent question.

    As far as I know, it's not possible to do in current version of Delphi. This is much unfortunate since those anonymous procedures would be great to have for quickly setting up an object's event handlers, for example when setting up test fixtures in a xUnit kind of automatic testing framework.

    There should be two ways for CodeGear to implement this feature:

    1: Allow for creation of anonymous methods. Something like this:

    Button1.OnClick := procedure( sender : tobject ) of object begin
      ...
    end;
    

    The problem here is what to put as the self pointer for the anonymous method. One might use the self pointer of the object from which the anonymous method was created, but then one can only create anonymous methods from an object context. A better idea might be to simply create a dummy object behind the scenes to contain the anonymous method.

    2: Alternatively, one could allow Event types to accept both methods and procedures, as long as they share the defined signature. In that way you could create the event handler the way you want:

    Button1.OnClick := procedure( sender : tobject ) begin
      ...
    end;
    

    In my eyes this is the best solution.

    0 讨论(0)
  • 2020-12-24 04:14
    @Button1.OnClick := pPointer(Cardinal(pPointer( procedure (sender: tObject) 
    begin 
      ((sender as TButton).Owner as TForm).Caption := 'Freedom to anonymous methods!' 
    
    end )^ ) + $0C)^;
    

    works in Delphi 2010

    0 讨论(0)
  • 2020-12-24 04:14

    Its easy to extend the below to handle more form event types.

    Usage

    procedure TForm36.Button2Click(Sender: TObject);
    var
      Win: TForm;
    begin
      Win:= TForm.Create(Self);
      Win.OnClick:= TEventComponent.NotifyEvent(Win, procedure begin ShowMessage('Hello'); Win.Free; end);
      Win.Show;
    end;
    

    Code

    unit AnonEvents;
    
    interface
    uses
      SysUtils, Classes;
    
    type
      TEventComponent = class(TComponent)
      protected
        FAnon: TProc;
        procedure Notify(Sender: TObject);
        class function MakeComponent(const AOwner: TComponent; const AProc: TProc): TEventComponent;
      public
        class function NotifyEvent(const AOwner: TComponent; const AProc: TProc): TNotifyEvent;
      end;
    
    implementation
    
    { TEventComponent }
    
    class function TEventComponent.MakeComponent(const AOwner: TComponent;
      const AProc: TProc): TEventComponent;
    begin
      Result:= TEventComponent.Create(AOwner);
      Result.FAnon:= AProc;
    end;
    
    procedure TEventComponent.Notify(Sender: TObject);
    begin
      FAnon();
    end;
    
    class function TEventComponent.NotifyEvent(const AOwner: TComponent;
      const AProc: TProc): TNotifyEvent;
    begin
      Result:= MakeComponent(AOwner, AProc).Notify;
    end;
    
    end.
    
    0 讨论(0)
  • 2020-12-24 04:20

    In previous Delphi versions you could use a regular procedure as event handler by adding the hidden self pointer to the parameters and hard typecast it:

    procedure MyFakeMethod(_self: pointer; _Sender: TObject);
    begin
      // do not access _self here! It is not valid
      ...
    end;
    
    ...
    
    var
      Meth: TMethod;
    begin
      Meth.Data := nil;
      Meth.Code := @MyFakeMethod;
      Button1.OnClick := TNotifyEvent(Meth);
    end;
    

    I am not sure the above really compiles but it should give you the general idea. I have done this previously and it worked for regular procedures. Since I don't know what code the compiler generates for closures, I cannot say whether this will work for them.

    0 讨论(0)
提交回复
热议问题