Comment #0 by qs.il.paperinik — 2022-12-08T14:48:28Z
Currently, an enum type is defined with this syntax:
```d
enum Identifier { Value1, Value2, Value3 }
```
(Note: A trailing comma is allowed, but not required.)
It would be nice to allow semicolon syntax as well:
```d
enum Identifier { Value1; Value2; Value3; }
```
(Note: A semicolon for the last value is required.)
It would make meta-programming easier. A `static foreach` cannot generate a comma-separated list, but it can generate a list of declarations.
Otherwise, one has to use string mixins which – apart from being hard to read – have the limitation that naming symbols that are inaccessible due to visibility attributes (private, usually) can be fed into and used by a template via an alias, but using the name is a non-starter. (This is to prove that it solves an issue actually present in the language.)
Grammar changes:
```diff
EnumBody:
{ EnumMembers }
;
EnumMembers:
+ EnumCommaMembers
+ EnumSemicolonMembers
+
+ EnumCommaMembers:
EnumMember
EnumMember ,
EnumMember , EnumMembers
+ EnumSemicolonMembers:
+ EnumMember ;
+ EnumMember ; EnumSemicolonMembers
AnonymousEnumDeclaration:
enum : EnumBaseType { EnumMembers }
enum { EnumMembers }
enum { AnonymousEnumMembers }
AnonymousEnumMembers:
+ AnonymousCommaEnumMembers
+ AnonymousSemicolonEnumMembers
+ AnonymousCommaEnumMembers
AnonymousEnumMember
AnonymousEnumMember ,
AnonymousEnumMember , AnonymousEnumMembers
+ AnonymousSemicolonEnumMembers:
+ AnonymousEnumMember ;
+ AnonymousEnumMember ; AnonymousSemicolonEnumMembers
```
Comment #1 by dfj1esp02 — 2022-12-09T15:07:42Z
This would be DIP.
Comment #2 by maxhaton — 2022-12-15T03:13:52Z
What would a static foreach inside an enum (without use of mixin) be able to express?
Comment #3 by qs.il.paperinik — 2023-02-22T12:41:57Z
(In reply to mhh from comment #2)
> What would a static foreach inside an enum (without use of mixin) be able to
> express?
You can generate members based off of other members. It’s very similar to `static foreach` generating `case` labels in a `switch` statement. Note that you excluding mixin proves my point, that is a use case. A relevant observation is that how well string mixins can be understood correlates strongly with their length: A simple, single declaration string mixin inside a `static foreach` is vastly preferable to a string mixin creating the enum and its members in one go.
As another example, conditional compilation using `static if` can make members (un)available depending on compile-time information. There’s lots of potential examples where one would like that. An enum member can specify certain actions, some of which might not be available on e.g. certain architectures.
Comment #4 by qs.il.paperinik — 2023-02-22T12:45:26Z
(In reply to anonymous4 from comment #1)
> This would be DIP.
What is that assertion based on?
DIPs are for additions of major language features or changes that require deprecation.
An alternative syntax that is a pure addition does not warrant a DIP. It definitely would if we were to remove the existing `enum` syntax instead, but that’s not what I’m suggesting.
Comment #5 by robert.schadek — 2024-12-13T19:26:17Z