www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Looping over all enum values

reply "Mark Isaacson" <turck11 hotmail.com> writes:
Is there a mechanism that allows you to loop over all of an
enum's values? I've wanted to do so a couple times now for
CTFE/mixin templates.

I was able to loop over the underlying type when my enum was
integer-based, but I can't do that if the values are strings
(also that solution is quite hacky).

Perhaps there's a pattern involving __traits?
May 28 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 28 May 2014 at 20:19:45 UTC, Mark Isaacson wrote:
 Is there a mechanism that allows you to loop over all of an
 enum's values? I've wanted to do so a couple times now for
 CTFE/mixin templates.
__traits(allMembers) on the enum can do it or this can help too http://dlang.org/phobos/std_traits.html#EnumMembers
May 28 2014
parent "Mark Isaacson" <turck11 hotmail.com> writes:
On Wednesday, 28 May 2014 at 20:20:37 UTC, Adam D. Ruppe wrote:
 On Wednesday, 28 May 2014 at 20:19:45 UTC, Mark Isaacson wrote:
 Is there a mechanism that allows you to loop over all of an
 enum's values? I've wanted to do so a couple times now for
 CTFE/mixin templates.
__traits(allMembers) on the enum can do it or this can help too http://dlang.org/phobos/std_traits.html#EnumMembers
Brilliant! Thanks Adam! I actually checked your book before any other sources for this :)
May 28 2014