Bug 22008 – foreach over enum members

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-06-08T21:04:18Z
Last change time
2022-02-07T19:10:57Z
Keywords
pull
Assigned to
No Owner
Creator
Basile-z

Comments

Comment #0 by b2.temp — 2021-06-08T21:04:18Z
Given the declaration --- enum E {e1, e2} --- the following code --- foreach (v; E) { // use v } --- could be semantically equivalent to --- foreach (e; __traits(allMembers, E)) { auto v = __traits(getMember, E, e); { // use v } } ---
Comment #1 by dlang-bot — 2021-06-08T21:13:23Z
@TungstenHeart created dlang/dmd pull request #12650 "fix 22008 - allow to iterate over enum members without `__traits`" fixing this issue: - fix 22008 - allow to iterate over enum members without `__traits` Adds the support to recognize EnumDeclaration as forerach aggregate. The following code ```d foreach (e; __traits(allMembers, E)) { auto v = __traits(getMember, E, e); } ``` can be more simply written ```d foreach (v; E) {} ``` This saves AST space, copies, time spent in semanticTraits with very few new dmd code. https://github.com/dlang/dmd/pull/12650