digitalmars.D.learn - Putting things in an enum's scope
- pineapple (11/11) Apr 06 2016 Is there any way in D to define static methods or members within
- Adam D. Ruppe (3/5) Apr 06 2016 No. You could make a struct rather than an enum though with the
- Alex Parrill (5/17) Apr 06 2016 You can use UFCS:
- Mike Parker (4/16) Apr 06 2016 Java's enums are classes, which is why that works. Nothing in
- Mint (20/32) Apr 10 2016 You can't declare functions inside of an enum, however, you can
Is there any way in D to define static methods or members within an enum's scope, as one might do in Java? It can sometimes help with code organization. For example, this is something that coming from Java I'd have expected to be valid but isn't: enum SomeEnum{ NORTH, SOUTH, EAST, WEST; static int examplestaticmethod(in int x){ return x + 2; } } int z = SomeEnum.examplestaticmethod(2);
Apr 06 2016
On Wednesday, 6 April 2016 at 13:59:42 UTC, pineapple wrote:Is there any way in D to define static methods or members within an enum's scope, as one might do in Java?No. You could make a struct rather than an enum though with the methods, and a bunch of static things to simulate SomeEnum.NORTH.
Apr 06 2016
On Wednesday, 6 April 2016 at 13:59:42 UTC, pineapple wrote:Is there any way in D to define static methods or members within an enum's scope, as one might do in Java? It can sometimes help with code organization. For example, this is something that coming from Java I'd have expected to be valid but isn't: enum SomeEnum{ NORTH, SOUTH, EAST, WEST; static int examplestaticmethod(in int x){ return x + 2; } } int z = SomeEnum.examplestaticmethod(2);You can use UFCS: enum SomeEnum { NORTH, ... } int examplestaticmethod(in SomeEnum e) { return e+2; } SomeEnum.NORTH.examplestaticmethod();
Apr 06 2016
On Wednesday, 6 April 2016 at 13:59:42 UTC, pineapple wrote:Is there any way in D to define static methods or members within an enum's scope, as one might do in Java? It can sometimes help with code organization. For example, this is something that coming from Java I'd have expected to be valid but isn't: enum SomeEnum{ NORTH, SOUTH, EAST, WEST; static int examplestaticmethod(in int x){ return x + 2; } } int z = SomeEnum.examplestaticmethod(2);Java's enums are classes, which is why that works. Nothing in Java can exist outside of a class. D's enums are like those in C. They do not define objects, just constant values.
Apr 06 2016
On Wednesday, 6 April 2016 at 13:59:42 UTC, pineapple wrote:Is there any way in D to define static methods or members within an enum's scope, as one might do in Java? It can sometimes help with code organization. For example, this is something that coming from Java I'd have expected to be valid but isn't: enum SomeEnum{ NORTH, SOUTH, EAST, WEST; static int examplestaticmethod(in int x){ return x + 2; } } int z = SomeEnum.examplestaticmethod(2);You can't declare functions inside of an enum, however, you can emulate the behaviour using an anonymous enum inside of a struct or class. ie. struct SomeEnum { enum : SomeEnum { NORTH = SomeEnum("North"), EAST = SomeEnum("East"), SOUTH = SomeEnum("South"), WEST = SomeEnum("West") } const string name; static SomeEnum getWithName(string name) { // . . . } }
Apr 10 2016