www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile-time immutable AA?

reply "Syniurge" <syniurge gmail.com> writes:
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?
Aug 02 2013
next sibling parent reply "Syniurge" <syniurge gmail.com> writes:
Turning it into an enum works.

I still have no idea why assigning an AA literal to an immutable 
AA isn't acceptable, but since it's only used at compile time 
it's probably better to make it an enum.
Aug 02 2013
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 08/03/2013 03:50 AM, Syniurge wrote:
 Turning it into an enum works.

 I still have no idea why assigning an AA literal to an immutable AA
 isn't acceptable,
It is a bug in the compiler implementation
 but since it's only used at compile time it's probably
 better to make it an enum.
Indeed.
Aug 02 2013
prev sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Friday, 2 August 2013 at 21:57:14 UTC, Syniurge wrote:
 // 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"
 ];
Pretty sure you just need to start that with: private enum TyIdxtoDType = [
Aug 02 2013