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

Walter Bright newshound2 at digitalmars.com
Sat Nov 19 02:35:19 UTC 2022


Under "Prior Work", the C style of enums should be mentioned.

In C, enum members are *not* scoped, they are inserted into the same scope as 
the enum declaration:

     enum E { ex = 1; };

     void tab() {
         enum E x = ex; // ok
         enum E y = E.ex; // fail
     }

This behavior motivated a lot of C users (including me) to declare C enums thusly:

     enum E { Eex = 1; };

     void tab() {
         enum E x = Eex;
     }

where the enum member name was prefixed with the enum name. This behavior was 
carried over into C++.

But C++ tired of it, and eventually introduced:

     enum class E { ex = 1; };

     void tab() {
         E x = ex; // fail
         E y = E::ex; // ok
     }

ImportC works by (internally) translating ImportC enums to D thusly:

     enum E { ex = 1; }
     alias ex = E.ex;    // <= re-declaring ex into enclosing scope

     void tab() {
         E x = ex; // ok
     }

All this leads to another way to get the result for D than using `with`:

     enum E { ex = 1; }
     alias ex = E.ex;    // <= re-declaring ex into enclosing scope

     void tab() {
         E x = ex; // ok
         E y = E.ex;  // also ok
     }

While this is a bit clumsy when there are lot of enum members, it suggests a 
special syntax for making it easy:

     enum NewKeywordHere E { ex = 1; }

     void tab() {
         E x = ex; // ok
         E y = E.ex;  // also ok
     }

I.e. going at this problem the opposite way C++ did.

I submit this is less disruptive to the language than adding $ and new lookup 
rules. It also puts the choice into the hands of the designer of the enum rather 
than the user of the enum. Isn't it better to make this a choice the designer 
should have?

It inherently makes this an error:

     enum NewKeywordHere E { ex = 1; }
     int ex = 6; // error, ex is already defined

as it should be.


More information about the Digitalmars-d mailing list