www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Variant confusion

reply "Peter Sommerfeld" <noreply rubrica.at> writes:
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
Mar 11 2013
parent reply "Jesse Phillips" <jessek.phillips+D gmail.m> writes:
On Monday, 11 March 2013 at 14:23:41 UTC, Peter Sommerfeld wrote:
 A confusing example:
 ----------------------------------------
 import std.stdio, std.variant, std.conv;

 alias Value = Variant;
 alias List = Value[];	
 alias Map = List[Value];
Oh, the new alias syntax is in? The problem you are seeing is that Variant can hold anything, that incudes a map. But since Variant is just a struct it is a different type from struct[struct] which is the type for map.
Mar 11 2013
parent reply "Peter Sommerfeld" <noreply rubrica.at> writes:
Jesse Phillips wrote:

 On Monday, 11 March 2013 at 14:23:41 UTC, Peter Sommerfeld wrote:
 A confusing example:
 ----------------------------------------
 import std.stdio, std.variant, std.conv;

 alias Value = Variant;
 alias List = Value[];	
 alias Map = List[Value];
Oh, the new alias syntax is in?
Hmmm, I'm new to D, why should I use the old one? And I like the new one more too, it is more descriptive IMHO.
 The problem you are seeing is that Variant can hold anything, that  
 incudes a map. But since Variant is just a struct it is a different type  
 from struct[struct] which is the type for map.
Seems I have to dig into the implementation for a workaround or use my own union to integrate the types. Thanks, Peter
Mar 11 2013
next sibling parent reply "cal" <callumenator gmail.com> writes:
On Monday, 11 March 2013 at 15:25:55 UTC, Peter Sommerfeld wrote:
 Seems I have to dig into the implementation for a workaround  
 or use
 my own union to integrate the types.

 Thanks, Peter
To get the held type of a Variant, use .type(): if(value.type() == typeid(Map)) writeln("value == map"); else if(value.type() == typeid(Value)) writeln("value == Value"); // Is still Value! prints map.
Mar 11 2013
parent "Peter Sommerfeld" <noreply rubrica.at> writes:
cal <callumenator gmail.com> wrote:

 To get the held type of a Variant, use .type():

 if(value.type() == typeid(Map))
     writeln("value == map");
 else if(value.type() == typeid(Value))
     writeln("value == Value"); // Is still Value!

 prints map.
Thanks cal, that will help! Seems I'm currently (?) a bit blind... Peter
Mar 11 2013
prev sibling next sibling parent reply Nick Treleaven <ntrel-public yahoo.co.uk> writes:
On 11/03/2013 15:25, Peter Sommerfeld wrote:
 alias Value = Variant;
 alias List = Value[];
 alias Map = List[Value];
Oh, the new alias syntax is in?
Hmmm, I'm new to D, why should I use the old one? And I like the new one more too, it is more descriptive IMHO.
The new syntax is fine, and to be preferred.
Mar 11 2013
parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Monday, March 11, 2013 17:22:26 Nick Treleaven wrote:
 On 11/03/2013 15:25, Peter Sommerfeld wrote:
 alias Value = Variant;
 alias List = Value[];
 alias Map = List[Value];
Oh, the new alias syntax is in?
Hmmm, I'm new to D, why should I use the old one? And I like the new one more too, it is more descriptive IMHO.
The new syntax is fine, and to be preferred.
Both will work, so it's really a matter of personal preference. Quite a few people prefer the new syntax though. - Jonathan M Davis
Mar 11 2013
prev sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Monday, 11 March 2013 at 15:25:55 UTC, Peter Sommerfeld wrote:
 Jesse Phillips wrote:
 Oh, the new alias syntax is in?
Hmmm, I'm new to D, why should I use the old one? And I like the new one more too, it is more descriptive IMHO.
I just didn't know it made it in yet, so many changes going into release nowadays.
 Seems I have to dig into the implementation for a workaround  
 or use
 my own union to integrate the types.
Sorry should have mentioned Variants track their stored type. Glad you got it though.
Mar 11 2013