How to use the result of __traits( allMembers , T ) with string mixins ?

ParticlePeter via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 28 06:52:51 PDT 2014


DMD tells me "Error: variable m cannot be read at compile time", 
but why ?

[code]
struct MyStruct {
	float float_value = 0.0f ;
	ubyte ubyte_value = 2 ;
}

enum members = [ __traits( allMembers , MyStruct ) ] ;

foreach( m ; members )  {
	mixin( "writeln( " ~ m ~ " , \" : \" , ( MyStruct." ~ m ~ 
".offsetof ) ;" ) ;
}
[\code]

I also tried ref m and foreach( i ; 0..members.length ) with 
m[i]. A simple writeln( m or m[i] ) always  worked.
I read the limitation of "String Mixins and Compile Time Function 
Execution" here: http://dlang.org/function.html#interpretation
But it doesn't make sense to me as members are enum values and 
known at compile time.

What am I doing wrong, and how could it be done ?

Regards, ParticlePeter


More information about the Digitalmars-d-learn mailing list