digitalmars.D.learn - iterate traits ?
- ddos (18/18) Aug 19 2014 Hi,
- Justin Whear (5/10) Aug 19 2014 Take a look at this example:
- ddos (2/15) Aug 19 2014 thank you!
- Philippe Sigaud via Digitalmars-d-learn (12/14) Aug 19 2014 Yeah, I think you transformed it into a runtime array by using [ ...
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
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
On Tuesday, 19 August 2014 at 18:25:24 UTC, Justin Whear wrote:On Tue, 19 Aug 2014 18:15:33 +0000, ddos wrote:thank you!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
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); } }A foreach becomes compile-time whenever the aggregate is a purely compile- time construct such as a tuple.
Aug 19 2014