DIPX: Enum Literals / Implicit Selector Expression

Paul Backus snarwin at gmail.com
Thu Dec 2 20:38:08 UTC 2021

On Thursday, 2 December 2021 at 19:44:07 UTC, russhy wrote:
> # DIPX: Enum Literals / Implicit Selector Expression
> Link: 
> https://gist.github.com/RUSShyTwo/2fcf6d294d25ba748d61927b5eed2944

First impressions:

enum MyTag
     A, B, C
MyTag tag = .A;

This looks like it can be made to work very easily, although I am 
not convinced it is a huge improvement compared to just using 

auto tag = MyTag.A;

Meanwhile, this:

void what(MyTag tag)

...is potentially troublesome, because it is grammatically 
ambiguous with the [module scope operator][1]. For example:

enum MyTag { A, B, C }
int B = 42;
void what(MyTag) {}

void example()
     // Error: function `what` is not callable using argument 
types `(int)`

We would need some rule like, "if an expression of the form 
`.Identifier` fails to compile, the compiler will attempt to 
rewrite it as `EnumName.Identifier`, where `EnumName` is the name 
of an enum visible in the current scope."

This raises several follow-on questions:

* What do we do if there are multiple enums with matching members?
* What if one of those enums is in the current module, and one is 
* Do we need a rule to prevent enum members from being "hijacked" 
by global variables?
* When instantiating templates, do we search enum types passed as 
template parameters, or just enums in the template declaration's 
lexical scope?

[1]: https://dlang.org/spec/module.html#module_scope_operators

More information about the Digitalmars-d mailing list