www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Iterate over enum

reply "HaraldZealot" <harald_zealot tut.by> writes:
Is it possible iterate over enum (preferable in compile time) or 
at least check that particular value belong to enum?
Apr 18 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/18/2015 01:30 PM, HaraldZealot wrote:
 Is it possible iterate over enum (preferable in compile time) or at
 least check that particular value belong to enum?
EnumMembers: http://dlang.org/phobos/std_traits.html#EnumMembers It returns a "static tuple", meaning that a foreach over those members will be a compile-time foreach. (No loop at all at run-time, the body is unrolled for every member.) Ali
Apr 18 2015
parent reply "HaraldZealot" <harald_zealot tut.by> writes:
On Saturday, 18 April 2015 at 20:42:09 UTC, Ali Çehreli wrote:
 On 04/18/2015 01:30 PM, HaraldZealot wrote:
 Is it possible iterate over enum (preferable in compile time) 
 or at
 least check that particular value belong to enum?
EnumMembers: http://dlang.org/phobos/std_traits.html#EnumMembers It returns a "static tuple", meaning that a foreach over those members will be a compile-time foreach. (No loop at all at run-time, the body is unrolled for every member.) Ali
Many thanks
Apr 18 2015
parent "Per =?UTF-8?B?Tm9yZGzDtnci?= <per.nordlow gmail.com> writes:
On Saturday, 18 April 2015 at 21:11:28 UTC, HaraldZealot wrote:
 On Saturday, 18 April 2015 at 20:42:09 UTC, Ali Çehreli wrote:
 On 04/18/2015 01:30 PM, HaraldZealot wrote:
 Is it possible iterate over enum (preferable in compile time) 
 or at
 least check that particular value belong to enum?
EnumMembers: http://dlang.org/phobos/std_traits.html#EnumMembers It returns a "static tuple", meaning that a foreach over those members will be a compile-time foreach. (No loop at all at run-time, the body is unrolled for every member.) Ali
Many thanks
If you want a dynamic version use [EnumMembers!T] or without enumerator aliases (enumerator value duplicate) import std.traits: EnumMembers; import std.algorithm: sort, uniq; return [EnumMembers!T].sort().uniq; I've turned this into enumMembers at https://github.com/nordlow/justd/blob/master/traits_ex.d#L396 This prevents the foreach loop from being inlined. I've had problems with compilation performance with nested foreach-iterating over EnumMembers!T with 100s of elements.
Apr 19 2015