enum and tuples

captaindet 2krnk at gmx.net
Thu Aug 8 23:16:12 PDT 2013


hi,

i am still struggling getting to grips with tuples, especially typetuples. i got into this when trying module introspection and the surprise discovery that __traits(allMembers, .) seems to return a typetuple of strings. (the tuple article on dlang and philippe's template tutorial do help a bit, but only so much.)

typetuples seem to live in a shadow world and quite obscurely should be referred to/passed around via aliases. even though we have alias a = b now i regard this as an irregularity/oddity of the language, i.e. that they are not treated the same way as variables/aggregates. anyhow, i started playing a bit putting them in enums anyway. surprisingly, "storing" tuples and typetuples in enums is accepted by the compiler. however, while enum tuples seem to behave normal/useful at compile time and run time, enum'ed typetuples don't like compile time, but seem to behave at runtime. (i rather would have expected the other way around). examples further below.

so i guess my questions are
a) if we are not allowed to put typetuples in enums, why is it not an error?
b) if enums are supposed to handle typetuples, why is it not working at compile time?

/det

module demo;
import std.stdio, std.typetuple, std.typecons;

enum ok = tuple("one", "two", "three");
pragma(msg, ok, ", ", ok[0]);
	// Tuple("one", "two", "three"), one
enum er = TypeTuple!("one", "two", "three");
//  pragma(msg, er);
	// Error: variable _er_field_0,1,2 cannot be read at compile time

void main(){
	writeln("ok: ", ok, " ok[0]: ", ok[0]);
		// ok: Tuple!(string, string, string)("one", "two", "three") ok[0]: one
	writeln("er: ", er, " er[0]: ", er[0]);
		// er: onetwothree er[0]: one
}


More information about the Digitalmars-d-learn mailing list