Enums - probably an old subject
Daniel Kozak
kozzi11 at gmail.com
Thu Nov 21 00:04:11 PST 2013
On Thursday, 21 November 2013 at 07:22:39 UTC, Steve Teale wrote:
> import std.stdio;
>
> enum Intention
> {
> EVIL,
> NEUTRAL,
> GOOD,
> SAINTLY
> }
>
> void foo(Intention rth)
> {
> if (rth == EVIL)
> writeln("Road to hell");
> }
>
>
> void main()
> {
> foo(EVIL);
> }
>
>
> Why does the compiler complain in both places about EVIL. Can
> it not work out which EVIL I mean? There's only one choice.
I don't think it would be a good idea to let a compiler decide
which symbol I mean :). So you must use Intention.EVIL instead of
just EVIL. Or you can do some trick like this:
enum Intention : int {_}
enum : Intention
{
EVIL = cast(Intention)0,
NEUTRAL = cast(Intention)1,
GOOD = cast(Intention)2,
SAINTLY = cast(Intention)3,
}
void foo(Intention rth)
{
if (rth == EVIL)
writeln("Road to hell");
}
void main()
{
foo(EVIL);
}
or use aliases:
enum Intention
{
EVIL,
NEUTRAL,
GOOD,
SAINTLY,
}
alias EVIL = Intention.EVIL;
alias NEUTRAL = Intention.NEUTRAL;
alias GOOD = Intention.GOOD;
alias SAINTLY = Intention.SAINTLY;
void foo(Intention rth)
{
if (rth == EVIL)
writeln("Road to hell");
}
void main()
{
foo(EVIL);
}
More information about the Digitalmars-d
mailing list