Putting things in an enum's scope

Alex Parrill via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 6 14:35:33 PDT 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();


More information about the Digitalmars-d-learn mailing list