www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is hash iteration cast required

And if so is it safe. Without the postblit on X there is no 
issue. With the postblit I get that X.__postblit is not callable, 
indicating that somehow values are being copied even though my 
hope was they would not since it is ref val. Unfortunately const 
ref val is not accepted by the compiler. Assuming I do nothing to 
val in the loop (in fact I'd like to just cast it to const to 
guarantee it), is this the way to go?

Thanks
Dan
---------------------
import std.traits;
alias const(X)[string] Map;
struct X {
   this(this) {}
   char[] x;
}

void foo(ref const(Map) m) {
   // FAILS TO COMPILE foreach(key, ref val; m) {
   foreach(key, ref val; cast(DeepUnqual!(typeof(m)))m) {
     pragma(msg, typeof(key));
   }
}

void main() {
   Map map;
   foo(map);
}

template DeepUnqual(T) {
   static if(isAssociativeArray!T) {
     alias Unqual!(Unqual!(ValueType!T)[Unqual!(KeyType!T)]) 
DeepUnqual;
   } else static if(isDynamicArray!T) {
     alias Unqual!(Unqual!(ArrayElementType!T)[]) DeepUnqual;
   } else static if(isPointer!T) {
     alias Unqual!(PointerTarget!T) * DeepUnqual;
   } else {
     alias Unqual!T DeepUnqual;
   }
}
Nov 15 2012