www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - iterate traits ?

reply "ddos" <oggs gmx.at> writes:
Hi,

i'm trying to create a dynamic vertex format for opengl, defined 
at compiletime by a struct

e.g. struct Vertex {float[3] position, float[3] normal}

i can get the name of all members with this:
auto b = [ __traits(allMembers, VertexType) ];

but i can't iterate them at compiletime because there is no 
static foreach?
foreach(e; b) {
   alias tt = typeof(__traits(getMember, VertexType, e)); // not 
working
   writeln(tt.sizeof);
}

since i need to setup vertexpointers for opengl at runtime my 
next question? - is it possible to evaluate the traits also at 
runtime? but i'd also like to know how i can iterate them at 
compiletime

thx in advance :)
Aug 19 2014
parent reply Justin Whear <justin economicmodeling.com> writes:
On Tue, 19 Aug 2014 18:15:33 +0000, ddos wrote:

 since i need to setup vertexpointers for opengl at runtime my next
 question? - is it possible to evaluate the traits also at runtime? but
 i'd also like to know how i can iterate them at compiletime
 
 thx in advance :)
Take a look at this example: http://dpaste.dzfl.pl/2fdea78a49b6 A foreach becomes compile-time whenever the aggregate is a purely compile- time construct such as a tuple.
Aug 19 2014
parent reply "ddos" <oggs gmx.at> writes:
On Tuesday, 19 August 2014 at 18:25:24 UTC, Justin Whear wrote:
 On Tue, 19 Aug 2014 18:15:33 +0000, ddos wrote:

 since i need to setup vertexpointers for opengl at runtime my 
 next
 question? - is it possible to evaluate the traits also at 
 runtime? but
 i'd also like to know how i can iterate them at compiletime
 
 thx in advance :)
Take a look at this example: http://dpaste.dzfl.pl/2fdea78a49b6 A foreach becomes compile-time whenever the aggregate is a purely compile- time construct such as a tuple.
thank you!
Aug 19 2014
parent Philippe Sigaud via Digitalmars-d-learn writes:
 A foreach becomes compile-time whenever the aggregate is a purely compile-
 time construct such as a tuple.
Yeah, I think you transformed it into a runtime array by using [ ... ]. Tuples with compatible types can be 'downgraded' to arrays that way. But there is no need to: tuples are iterable, indexable and slice-able in D. import std.stdio; struct Vertex {float[3] position; float[3] normal;} void main() { foreach(e; __traits(allMembers, Vertex)) { alias tt = typeof(__traits(getMember, Vertex, e)); // worksforme writeln(tt.sizeof); } }
Aug 19 2014