enum behaivor. another rehash of the topic

monarch_dodra monarchdodra at gmail.com
Sun Dec 15 04:36:56 PST 2013


On Sunday, 15 December 2013 at 11:27:51 UTC, bearophile wrote:
> By the way, I am not criticizing "D verbose approach". I have 
> criticized the weak typing:
>
> enum Foo { good, bad }
> void main() {
>     int x = Foo.good; // Weak typing.
> }

Hum... Well, it's not actually weak typing. It's strong typing 
with implicit cast *to* the base type (int by default).

For example:

enum Foo
{
     a,
     b,
     c,
}

void foo(Foo)
{}

void main()
{
     foo(1); //Nope. I want a Foo.
}

Whether this is a good or bad thing I don't know. If D where 
"just" D, I'd say it's a bad thing (it should require an explicit 
cast). However, arguably, there might be enough C heritage in D 
to justify it.

As long as we don't have "int to enum" implicit conversion, I 
think it's fine.

> And some people have criticized the verbosity in special 
> situations, like this:
>
>
> enum Foo { good, bad }
> void bar(Foo f) {}
> void main() {
>     // bar(bad); // Not enough
>     bar(Foo.bad);
> }

I am of those that think this is a good thing.

> Bye,
> bearophile


More information about the Digitalmars-d mailing list