Is there a way to create compile-time delegates?

Philippe Sigaud philippe.sigaud at gmail.com
Mon Jul 19 13:34:00 PDT 2010


On Mon, Jul 19, 2010 at 22:01, torhu <no at spam.invalid> wrote:

>
>
> I wasn't able to make it work.


Me too :(


> The compiler probably sees delegates as something that just can't be
> created at compile time, since no runtime contexts exist then.  Which is
> reasonable.
>

Can you initialize pointers in general, at compile-time?



> Maybe one of those templates that turn functions into delegates will work?
>


I had the same idea and tried to use std.functional.toDelegate, but to no
avail.

enum moo = ()  {return 1;};

struct foo {
   int delegate( ) dg = toDelegate(moo);
}

Error: forward reference to inferred return type of function call
toDelegate(delegate int()|

int moo() { return 1;}

struct foo {
   int delegate( ) dg = toDelegate(&moo);
}

Error: forward reference to inferred return type of function call
toDelegate((& moo))|

'auto' strikes again :(
I came to hate these forward reference errors. That and the fact that auto
functions do not appear in the docs, that's enough for me to avoid auto as
much as possible for functions.



> Otherwise I guess you're back to using a factory function for initializing
> instances.
>
> Maybe just checking for null pointers before calling those delegates ends
> up being the easiest solution.
>


Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100719/8e876d14/attachment.html>


More information about the Digitalmars-d-learn mailing list