Thread overview
Re: How to make delegate refer to itself?
Nov 23, 2013
Philippe Sigaud
Nov 23, 2013
H. S. Teoh
Nov 23, 2013
H. S. Teoh
November 23, 2013
On Sat, Nov 23, 2013 at 11:57 PM, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote:
> void delegate(Event) dg = (Event e) {
>                 if (e == ...)
>                         queue.remove(dg); // NG: Still complains 'dg' isn't defined
>         };
>         queue.register(dg);

Did you try this?

void delegate(Event) dg;

dg = (Event e) {
    if (e == ...)
        queue.remove(dg); // NG: Still complains 'dg' isn't defined
};

queue.register(dg);
November 23, 2013
On Sun, Nov 24, 2013 at 12:24:53AM +0100, Philippe Sigaud wrote:
> On Sat, Nov 23, 2013 at 11:57 PM, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote:
> > void delegate(Event) dg = (Event e) {
> >                 if (e == ...)
> >                         queue.remove(dg); // NG: Still complains 'dg' isn't defined
> >         };
> >         queue.register(dg);
> 
> Did you try this?
> 
> void delegate(Event) dg;
> 
> dg = (Event e) {
>     if (e == ...)
>         queue.remove(dg); // NG: Still complains 'dg' isn't defined
> };
> 
> queue.register(dg);

You're right, separating the dg declaration from the body of the delegate fixes the problem.

But still, it's rather ugly. I wish there were a way for a delegate to refer to itself.


T

-- 
Frank disagreement binds closer than feigned agreement.
November 23, 2013
On Sun, Nov 24, 2013 at 12:15:23AM +0100, lomereiter wrote:
> Why so much fuss about delegates? I would simply define an interface.
[...]

I know I can do that (and in fact it's what I had before).

But classes and interfaces are rather heavy (more indirection, allocates vtables, etc.), and require the declaration of tiny classes everywhere, which is a pain when I have many tiny code snippets I wish to register and unregister in various places. Using delegates is much cleaner (thanks to lambda syntax) and lighter weight, especially when I need to close over local variables.


T

-- 
May you live all the days of your life. -- Jonathan Swift