Variant confusion

Peter Sommerfeld noreply at rubrica.at
Mon Mar 11 07:23:40 PDT 2013


A confusing example:
----------------------------------------
import std.stdio, std.variant, std.conv;

alias Value = Variant;
alias List = Value[];	
alias Map = List[Value];

void main(string[] args){
     Value value = "abc";
     Map	 map = [value:[]];
     Value key = value; // save value

     // expect value to become of type Map
     value = map;
	
     if(typeid(value) == typeid(Map))
         writeln("value == map");
     else if(typeid(value) == typeid(Value))
         writeln("value == Value"); // Is still Value!
	
     // add some values to map, does not work with value.	
     map[key] ~= to!Value(133);
     map[key] ~= to!Value("abc");
     map[key] ~= to!Value(1.23456);

     // Voila! Both show the same value!!!

     writeln(value, " == ", map);	
}

What is going on here? If value shows the same value as
map it should be one. But is not a Map, still a Value.
Or do i miss here something importand ?

Peter


More information about the Digitalmars-d-learn mailing list