www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can I count the of enum's fields at compile time?

reply Marc <jckj33 gmail.com> writes:
for example:

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = coutOfFields(A); // 5 fields
Nov 22
next sibling parent reply Michael V. Franklin <slavo5150 yahoo.com> writes:
On Thursday, 23 November 2017 at 00:58:21 UTC, Marc wrote:
 for example:

 enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

 enum int countOfA = coutOfFields(A); // 5 fields
https://dlang.org/spec/traits.html#allMembers enum A { a = -10, b = -11, c = -12, d = -13, e = -34} enum int countOfA = __traits(allMembers, A).length; // 5 fields static assert(countOfA == 5); Mike
Nov 22
parent Marc <jckj33 gmail.com> writes:
On Thursday, 23 November 2017 at 01:01:42 UTC, Michael V. 
Franklin wrote:
 On Thursday, 23 November 2017 at 00:58:21 UTC, Marc wrote:
 for example:

 enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

 enum int countOfA = coutOfFields(A); // 5 fields
https://dlang.org/spec/traits.html#allMembers enum A { a = -10, b = -11, c = -12, d = -13, e = -34} enum int countOfA = __traits(allMembers, A).length; // 5 fields static assert(countOfA == 5); Mike
This was fast! Thanks
Nov 22
prev sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Thursday, November 23, 2017 00:58:21 Marc via Digitalmars-d-learn wrote:
 for example:

 enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

 enum int countOfA = coutOfFields(A); // 5 fields
import std.traits; enum countOfA = EnumMembers!A.length; - Jonathna M Davis
Nov 22
parent reply Marc <jckj33 gmail.com> writes:
On Thursday, 23 November 2017 at 01:04:29 UTC, Jonathan M Davis 
wrote:
 On Thursday, November 23, 2017 00:58:21 Marc via 
 Digitalmars-d-learn wrote:
 for example:

 enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

 enum int countOfA = coutOfFields(A); // 5 fields
import std.traits; enum countOfA = EnumMembers!A.length; - Jonathna M Davis
This sounds more readable. I was going to write a "function extension" to enum but I think it isn't really needed. Thank you too.
Nov 22
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/22/2017 05:21 PM, Marc wrote:
 On Thursday, 23 November 2017 at 01:04:29 UTC, Jonathan M Davis wrote:
 On Thursday, November 23, 2017 00:58:21 Marc via Digitalmars-d-learn 
 wrote:
 for example:

 enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

 enum int countOfA = coutOfFields(A); // 5 fields
import std.traits; enum countOfA = EnumMembers!A.length; - Jonathna M Davis
This sounds more readable. I was going to write a "function extension" to enum but I think it isn't really needed. Thank you too.
As an eponymous template: enum One { a } enum Three { a, b, c } import std.range : EnumMembers; enum countOf(E) = EnumMembers!E.length; unittest { static assert(countOf!One == 1); static assert(countOf!Three == 3); } void main() { } Ali
Nov 22
parent Marc <jckj33 gmail.com> writes:
On Thursday, 23 November 2017 at 01:34:54 UTC, Ali Çehreli wrote:
 On 11/22/2017 05:21 PM, Marc wrote:
 On Thursday, 23 November 2017 at 01:04:29 UTC, Jonathan M 
 Davis wrote:
 On Thursday, November 23, 2017 00:58:21 Marc via 
 Digitalmars-d-learn wrote:
 [...]
import std.traits; enum countOfA = EnumMembers!A.length; - Jonathna M Davis
This sounds more readable. I was going to write a "function extension" to enum but I think it isn't really needed. Thank you too.
As an eponymous template: enum One { a } enum Three { a, b, c } import std.range : EnumMembers; enum countOf(E) = EnumMembers!E.length; unittest { static assert(countOf!One == 1); static assert(countOf!Three == 3); } void main() { } Ali
whoa, this is so easy and elegant. I'm falling in love with D. Thank you too!
Nov 22