No we should not support enum types derived from strings

Steven Schveighoffer schveiguy at gmail.com
Fri May 7 15:51:39 UTC 2021


On 5/7/21 11:30 AM, Andrei Alexandrescu wrote:
> On 5/7/21 11:20 AM, Steven Schveighoffer wrote:
>> If you mean we shouldn't support it (as an ambiguous case) in 
>> *conversion* utilities (i.e. to/from string), then this makes some 
>> sense. But it's also not straightforward. Sometimes you WANT to 
>> convert from the enum to the base type. Sometimes you want to convert 
>> to the enum name. Going backwards (string to enum), which one makes 
>> more sense? It depends on context. It also doesn't help that a string 
>> enum implicitly converts to a string. The language is going to 
>> circumvent any policies Phobos has on that front.
> 
> Enums are poorly designed, but that's only a small part of the problem.
> 
> The bigger problem is the corruption of a noble principle. We wanted to 
> be as generic as possible, and indeed in the beginning that seemed not 
> only possible, but also easy. I don't think there's any other language 
> or library supporting different character widths with this little 
> aggravation.
> 
> Then this whole "be as generic as possible" became a slippery slope of 
> inclusion. Allow enum strings. Allow alias this strings.

But an enum with base string type can be passed as a string. The PR in 
question is working around a limitation of the Phobos trait that says 
something derived from a string isn't really usable as a string (when it 
is).

The problem I see is, when phobos says something isn't true, when it 
really is, causes no end of confusion (*cough* autodecoding)

static assert(!isSomeString!T);
// yet...
string s = someT;

> 
> How about no.
> 
> User: "I have this enum string str and phobos won't consider it a 
> string. Help!"
> 
> Another user: "Just use str.representation if you want to pass str 
> around as a string."
> 

User: "OK, but when should I use representation? I already pass it 
around as a string and it works fine. Why can't phobos comprehend that, 
when the language has no problems with it?"

-Steve


More information about the Digitalmars-d mailing list