Thread overview
Using enums as function parameters (in a minimized way)
Dec 01, 2015
Ozan
Dec 01, 2015
Rikki Cattermole
Dec 01, 2015
tcak
Dec 01, 2015
tcak
Dec 01, 2015
Daniel Kozak
December 01, 2015
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

December 01, 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;	
}
December 01, 2015
V Tue, 01 Dec 2015 10:44:06 +0000
Ozan via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
napsáno:

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

with(SomethingAboutChristmas) {
    goingChristmas(Tree);
    goingChristmas(SantaClaus);
    ...
}

December 01, 2015
On Tuesday, 1 December 2015 at 10:50:04 UTC, Rikki Cattermole wrote:
> 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;	
> }

This is like: Q) I want to write an OS. How?  A) Write in assembly.

What Ozan says is logical. Compiler should assume it in that way normally. I have thoroughly thought about whether this assumption would cause problem yet though.

Unfortunately compiler doesn't accept that.
December 01, 2015
On Tuesday, 1 December 2015 at 13:03:37 UTC, tcak wrote:
> On Tuesday, 1 December 2015 at 10:50:04 UTC, Rikki Cattermole wrote:
>> [...]
>
> This is like: Q) I want to write an OS. How?  A) Write in assembly.
>
> What Ozan says is logical. Compiler should assume it in that way normally. I have thoroughly thought about whether this assumption would cause problem yet though.
>
> Unfortunately compiler doesn't accept that.

*haven't thought