Enum literals, good? bad? what do you think?

Paul Backus snarwin at gmail.com
Thu Jul 22 14:58:12 UTC 2021


On Thursday, 22 July 2021 at 14:05:53 UTC, Dukc wrote:
> On Thursday, 22 July 2021 at 03:28:44 UTC, Paul Backus wrote:
>> ```d
>> enum E : string { x = "y", y = "x" }
>>
>> E e = "y"; // what happens here?
>> ```
>
> The VRP mechanics do not currently let that compile, so nothing 
> will break. it would be equal to `E e = E.y`; `E e = 
> cast(E)"y"` on the other hand would still be `E e = E.x`, as 
> it's now.
>
> Now, I admit that difference between implicit and explicit cast 
> is confusing and a weakness.  Maybe the syntax should be `E e = 
> ".y"` instead to avoid the difference in behaviour between 
> implicit and explicit cast.

Ok, now imagine you're new to D and don't know about this 
feature. What are you going to *think* `E e = ".y"` does?

Using string-literal syntax for things that aren't actually 
string literals is just asking for confusion.


More information about the Digitalmars-d mailing list