No we should not support enum types derived from strings

deadalnix deadalnix at gmail.com
Mon May 10 00:39:13 UTC 2021


On Friday, 7 May 2021 at 15:33:56 UTC, Adam D. Ruppe wrote:
> On Friday, 7 May 2021 at 15:25:30 UTC, Andrei Alexandrescu 
> wrote:
>> Enums derived from strings should not be supported as strings 
>> in the standard library.
>
> I don't think the stdlib should special case much of anything.
>
> Special casing enums is a mistake. If the user wants it treated 
> as a string, they can cast it to a string.
>
> [...]
>
> Kill all the special cases!

100% agreed, but, back to my original point, why is the enum 
thing a special case to begin with?

The fact that it is a special case to begin with flies in the 
face of Liskov's substitution principle - the enum type clearly 
is a subtype of string.

You got to wonder how it came to be that it just don't work 
automatically to begin with. Adding special cases is indeed the 
wrong path. There is something deeper rotten here, and just 
saying, no, this shouldn't work is just not cutting it.

Note that there should be special cases, but it's be good to 
understand why these are special case to begin with, and fix this.

Alternatively, we decide enums are not subtypes, in which case 
they shouldn't be implicitly convertible either. That wouldn't be 
such a bad idea as I've often missed the ability to do opaque 
type aliasing in D, but that seems way more disruptive than just 
admitting that "enum strings" are indeed a subtype of string.


More information about the Digitalmars-d mailing list