Enum of functions?

IgorStepanov wazar at mail.ru
Mon Nov 25 16:27:24 PST 2013


On Monday, 25 November 2013 at 23:32:27 UTC, Chris Williams wrote:
> Is there any way to do something like this?
>
> import std.stdio;
>
> enum Foo : void function() {
> 	WOMBAT = () {writeln("Wombat");}
> }
>
> void doStuff(Foo f) {
> 	f();
> }
>
> int main() {
> 	doStuff( Foo.WOMBAT );
> 	
> 	return 0;
> }
>
> Currently, I get the errors:
>
> hello.d(4): Error: non-constant nested delegate literal 
> expression __lambda1
> hello.d(12): Error: delegate hello.Foo.__lambda1 is a nested 
> function and cannot be accessed from D main
>
> I can fix it by declaring a function outside of Foo and setting 
> WOMBAT = &fname, but it seems like I should be able to create a 
> hidden lambda.

You can write

enum Foo : void function()
{
     WOMBAT = function void () {writeln("Wombat");}
}

or

enum Foo
{
     WOMBAT = function void () {writeln("Wombat");}
}

`() {writeln("Wombat");}` literal recognized by compiler as 
delegate, not function.


More information about the Digitalmars-d-learn mailing list