DIPX: Enum Literals / Implicit Selector Expression

Walter Bright newshound2 at digitalmars.com
Mon Jul 4 22:34:01 UTC 2022


On 7/1/2022 3:48 PM, Steven Schveighoffer wrote:
> These are not unscoped enums. There is a fundamental disconnect with what I 
> wrote and what you are thinking I wrote. Swift's enums are not actually as loose 
> type-wise as D's. They cannot convert to anything implicitly.

I agree that disallowing implicit conversions is an important part of your proposal.

>>> Am I misunderstanding something?
>>
>> For one thing, you'll have to add all the members of all the enums to the 
>> symbol lookup.
> 
> No, you don't. You only need to resolve the names when you are checking whether 
> a function call matches, or an assignment/operation matches.

Of course, that precludes `typeof(a)`, `a.sizeof`, `a + 3`, function templates, 
variadic function parameters.

How do you distinguish an instance of an elem with the same name as an element 
of the enum?


> It's a combinatorial explosion *only if* you wrote function overloads for all 
> the possible combinations.

This implies deferring resolving expressions until trying to do matching, and 
constantly restarting that for each overload.


> C++ scoped enums are more like Swift in that they don't implicitly convert to 
> the base type.

I didn't know that.


>>> enum A { a, b, c }
>>> alias a = A.a;
>>> alias b = A.b;
>>> alias c = A.c;
>>>
>>> void bar(A a) {}
>>>
>>> void foo()
>>> {
>>>     int a = 5;
>>>     bar(a); // error;
>>> }
>>
>> Doesn't your proposal have the same error?
> 
> No

I understand what you're doing, but confess that makes me uncomfortable.


> I wrote this little piece of code to demonstrate a proof of concept feature 
> (based on existing D syntax). It only works for equality, but probably could be 
> extended for other operations. It will not work for the full features I am 
> looking for (i.e. assignment and implicit conversion for function parameters).
> 
> But it gives you an idea how you can defer the type checking until it's needed.

It's clever. With that, is a language change justified?


More information about the Digitalmars-d mailing list