Discussion Thread: DIP 1044--Enum Type Inference--Community Review Round 1

Timon Gehr timon.gehr at gmx.ch
Tue Nov 22 01:41:06 UTC 2022


On 22.11.22 01:49, Walter Bright wrote:
> On 11/20/2022 1:58 PM, Timon Gehr wrote:
>> On 20.11.22 22:32, Walter Bright wrote:
>>>
>>> But yes, it would be a (small) breaking change.
>>
>> I really wouldn't want to run into this:
>>
>> ```d
>> enum Role{
>>      guest,
>>      member,
>>      developer,
>> }
>>
>> void main(){
>>      Role r;
>>      writeln(r); // error
(Should perhaps be auto s = to!string(r);)
>> }
>> ```
>>
>> Too messy for me, but up to you.
> 
> I'm not seeing where the shadowing is?

That's indeed basically the point. It's invisible.

The implementation of to!string for enums has some code like this:


switch(e){
     foreach(member; EnumMembers!E){
         case member:
             ...
     }
}

https://github.com/dlang/phobos/blob/master/std/conv.d#L1065-L1070

This is where the shadowing happens. You then get an compile error in 
the guts of Phobos.

(I originally thought writeln would break due to that issue, but it's 
not using the same code:
https://github.com/dlang/phobos/blob/master/std/format/internal/write.d#L3023-L3025

I am not fully sure if this will break with implicit with, but I think 
the DIP would allow $val here if the enum has a "val" member. So if you 
want to cover an equal amount of ground to the DIP, this will actually 
break too.)

I think spilling the implementation details of library code into user 
code like that is a no-go.


More information about the Digitalmars-d mailing list