Compile-time immutable AA?
Syniurge
syniurge at gmail.com
Fri Aug 2 14:57:13 PDT 2013
This is my first code in D, I feel like I'm already abusing mixin
macros but I'm trying to avoid as much duplicate
code/redundancies as possible. I would love a "static switch" as
proposed in http://d.puremagic.com/issues/show_bug.cgi?id=6921 to
replace the first AA, but since there isn't atm, a compile-time
AA with a mixin is more elegant than seven "static if/else if" in
single file.
enum TypeIndex : uint {
Nil = 0, // Undefined or dynamic value, depending on the
context
Number,
String,
Table,
Classinst,
Function,
Bool // Only used internally to avoid unnecessary
bool->float conversions
}
// The rest below is for compile time only
private immutable string[TypeIndex] TyIdxtoDType = [
TypeIndex.Nil : "DynamicValue",
TypeIndex.Number : "Number",
TypeIndex.String : "string",
TypeIndex.Table : "TableHeader*",
TypeIndex.Classinst : "ClassHeader*",
TypeIndex.Function : "FunctionValue*",
TypeIndex.Bool : "bool"
];
private template DType(TypeIndex tyIdx) {
mixin("alias " ~ TyIdxtoDType(tyIdx) ~ " DType;");
}
But LDC gives me this error:
TalesRuntime.d(51): Error: expression
'[cast(TypeIndex)0u:"DynamicValue", cast(TypeIndex)1u:"Number",
cast(TypeIndex)2u:"string", cast(TypeIndex)3u:"TableHeader*",
cast(TypeIndex)4u:"ClassHeader*",
cast(TypeIndex)5u:"FunctionValue*", cast(TypeIndex)6u:"bool"]' is
not a constant
And if I remove the immutable qualifier it says that the AA isn't
available at compile time.
Probably related:
http://d.puremagic.com/issues/show_bug.cgi?id=6238 , but yebblies
says that associative arrays work fine in CTFE, so I don't
understand what I'm doing wrong. Is there a better way to create
an AA mapping an enum to D type names?
More information about the Digitalmars-d-learn
mailing list