digitalmars.D.learn - Enum of functions?
- Chris Williams (21/21) Nov 25 2013 Is there any way to do something like this?
- IgorStepanov (13/34) Nov 25 2013 You can write
- Chris Williams (4/16) Nov 25 2013 Thank you. It looks like 2.063 only handled the first case (and
- Shammah Chancellor (3/29) Nov 25 2013 What is the practical purpose of such a thing?
- Chris Williams (13/15) Nov 25 2013 The two cases I can think of are:
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.
Nov 25 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.
Nov 25 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.
Nov 25 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
Nov 25 2013
On Tuesday, 26 November 2013 at 05:13:00 UTC, Shammah Chancellor wrote:What is the practical purpose of such a thing? -ShammahThe 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.
Nov 25 2013