No we should not support enum types derived from strings

Steven Schveighoffer schveiguy at gmail.com
Fri May 7 17:11:32 UTC 2021


On 5/7/21 12:30 PM, Adam D. Ruppe wrote:
> On Friday, 7 May 2021 at 15:51:39 UTC, Steven Schveighoffer wrote:
>> But an enum with base string type can be passed as a string.
> 
> "Can be passed as a" is not the same as "is a". There's a conversion 
> involved.

But that's the intention of the function. format doesn't care what the 
expression really is, it wants some type of string.

How do you say "I want to accept something that's a string, but I want 
it as a string please"

> For better or for worse, D templates do not participate in conversion 
> and we shouldn't pretend that they do. This is often times very useful - 
> you don't want to lose information in many templates. But there's other 
> times when that information doesn't matter and it would be nice it you 
> didn't have to think about it....

e.g. format.

> ...so maybe we should consider changing templates so they can 
> participate at the language level... it would be interesting if the 
> compiler did the conversions BEFORE instantiating any template. Then it 
> can reuse the instances more easily too. I think it actually does for 
> const params for example, but it could do more.

Interesting idea!

> 
>> 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?"
> 
> But the language DOES have problems with it for certain types of 
> functions. Phobos is trying to deny that reality.

What I mean is, I can write:

void foo(string s);

and it works for enums that are string-based. Why doesn't format work 
with that same principle? The answer is because there isn't a good way 
to do it.

-Steve


More information about the Digitalmars-d mailing list