Using enums as function parameters (in a minimized way)

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 1 02:50:00 PST 2015


On 01/12/15 11:44 PM, Ozan wrote:
> Hi
>
> Let's say we have an enum like
>
> enum SomethingAboutChristmas {
>    SantaClaus,
>    Angel,
>    Tree
> }
>
> and want to use it in a function like
>
>    void goingChristmas(SomethingAboutChristmas enumvalue)
>
> it works fine like following
>
>    goingChristmas(SomethingAboutChristmas.Tree)
>
> I prefer to use a shorter version
>
>    goingChristmas(Tree)
>
> because it's clear (for me), that "Tree" in "goingChristmas()" is an
> enum of "SomethingAboutChristmas"
>
> Is any chance to do this? The DMD-compiler says no.
>
> Thanke & Regards, Ozan

If you insist..
You can also use alias and with statement to emulate this too.
Or generate it at compile time.

enum SomethingAboutChristmas {
     SantaClaus,
     Angel,
     Tree
}

enum {
     SantaClaus = SomethingAboutChristmas.SantaClaus,
     Angel = SomethingAboutChristmas.Angel,
     Tree = SomethingAboutChristmas.Tree,
}

void main() {
	SomethingAboutChristmas foo = Tree;	
}


More information about the Digitalmars-d-learn mailing list