digitalmars.D.learn - array of functions
- Trass3r (13/13) Jun 04 2009 I tried to use a function lookup table to quickly access a particular on...
- Jarrett Billingsley (7/12) Jun 04 2009 You've got it the other way around. If func is an instance method
- Trass3r (5/18) Jun 04 2009 I know, but if I change that array to delegates, dmd complains:
- Robert Clipsham (3/5) Jun 05 2009 I believe that LDC used to support this, but had to remove the
I tried to use a function lookup table to quickly access a particular one. But the compiler (dmd1) complains about Error: non-constant expression & func I guess because it's used inside a class since &func is of type function, not delegate? Here's a stripped down example: protected Object function(int)[] lookup = [ &func ]; protected Object func(int) { return null; }
Jun 04 2009
On Thu, Jun 4, 2009 at 12:42 PM, Trass3r <mrmocool gmx.de> wrote:I tried to use a function lookup table to quickly access a particular one. But the compiler (dmd1) complains about Error: non-constant expression & func I guess because it's used inside a class since &func is of type function, not delegate?You've got it the other way around. If func is an instance method (non-static method) of a class, &func is a delegate, not a function. It can't evaluate &func at compile-time because there is no way to create a delegate at compile-time. You need an instance to create a delegate, which you can only create by using 'new', which only works at runtime.
Jun 04 2009
Jarrett Billingsley schrieb:On Thu, Jun 4, 2009 at 12:42 PM, Trass3r <mrmocool gmx.de> wrote:I know, but if I change that array to delegates, dmd complains: Error: cannot implicitly convert expression (&func) of type Object function(int) to Object delegate(int)I tried to use a function lookup table to quickly access a particular one. But the compiler (dmd1) complains about Error: non-constant expression & func I guess because it's used inside a class since &func is of type function, not delegate?You've got it the other way around. If func is an instance method (non-static method) of a class, &func is a delegate, not a function.It can't evaluate &func at compile-time because there is no way to create a delegate at compile-time.Yeah, gotta use a different approach.
Jun 04 2009
Jarrett Billingsley wroteIt can't evaluate &func at compile-time because there is no way to create a delegate at compile-time.I believe that LDC used to support this, but had to remove the functionality due to some bugs with it/to be compatible with dmd.
Jun 05 2009