Thread overview
Enum of functions?
Nov 25, 2013
Chris Williams
Nov 26, 2013
IgorStepanov
Nov 26, 2013
Chris Williams
Nov 26, 2013
Shammah Chancellor
Nov 26, 2013
Chris Williams
November 25, 2013
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.
November 26, 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.
November 26, 2013
On Tuesday, 26 November 2013 at 00:27:25 UTC, IgorStepanov wrote:
> 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.

Thank you. It looks like 2.063 only handled the first case (and only if there was a single entry), but with 2.064 it works perfectly.

November 26, 2013
On 2013-11-25 23:32:25 +0000, Chris Williams said:

> 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.

What is the practical purpose of such a thing?

-Shammah

November 26, 2013
On Tuesday, 26 November 2013 at 05:13:00 UTC, Shammah Chancellor wrote:
> What is the practical purpose of such a thing?
>
> -Shammah

The two cases I can think of are:

1. To define a set of supported handlers which can be passed in as a parameter to a call. Rather than writing a switch in your method that calls the correct function, based on the supported values in the enum, you build the handlers right into the enum. It pulls your code into one.

2. If you have a quick and easy method for serializing an enum - for example, converting the name to a string and back - then you can write can write code that deserializes and calls a function in a single line.

But mostly, I was just curious.