No we should not support enum types derived from strings

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu May 13 02:13:04 UTC 2021


On Wednesday, May 12, 2021 7:16:42 PM MDT Andrei Alexandrescu via Digitalmars-
d wrote:
> I was all over run.dlang.org like "Sure that's not going to work... wait
> a second, it does! But that other thing's not going to work... what,
> that works too!" I didn't know D's enums are _that_ odd.
>
> It seems you can do almost everything with an enum that you can do with
> its base type. Keyword being "almost".

Yeah, if enums are supposed to only have a fixed set of values, then they're
completely broken. The language does almost nothing to guarantee it. One
result of that is that you have to be _very_ careful about how you use
something like final switch - especially since it's not checked with
-release.

Of course, if enums are just named values without caring about whether it's
possible to have an enum with a different value than the ones listed, then
the fact that the enum is even treated differently from the base type causes
other problems. So, ultimately, I think that D enums are pretty
schizophrenic and not particularly well-designed.

I've argued in the past that the language should disallow all operations on
enums (aside from casts) which aren't guaranteed to result in a valid value
for that enum type, but not everyone agrees with that stance.

- Jonathan M Davis





More information about the Digitalmars-d mailing list