views:

624

answers:

3

Hello,

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.. but is there a way to use a clouser on method pointers? and how to define if can?

eg :

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

Thanks!

+2  A: 

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.

Hans-Eric
A method doesn't have the same calling signature as a procedure with the same arguments. Methods always pass Self as a "hidden" argument.
Mason Wheeler
Yes, of course. But I see no reason why the compiler shouldn't be able to handle both cases "behind the scenes". It could for instance create a dummy class wrapper around the anonymous procedure if a method is expected.
Hans-Eric
+2  A: 

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.

dummzeuch
I just tested that, and it didn't work with a closure, but I may have missed something.
Jim McKeeth
+1  A: 

Allen Bauer had a very interesting series of blogs touching this area:

Multicast events using generics http://blogs.codegear.com/abauer/2008/08/15/38865

Multicast Events - the cleanup http://blogs.codegear.com/abauer/?p=38866

Multicast Events - the finale http://blogs.codegear.com/abauer/?p=38867