digitalmars.D.learn - Variant confusion
- Peter Sommerfeld (29/29) Mar 11 2013 A confusing example:
- Jesse Phillips (5/11) Mar 11 2013 Oh, the new alias syntax is in?
- Peter Sommerfeld (6/18) Mar 11 2013 Hmmm, I'm new to D, why should I use the old one? And I like
- cal (8/12) Mar 11 2013 To get the held type of a Variant, use .type():
- Peter Sommerfeld (4/10) Mar 11 2013 Thanks cal, that will help! Seems I'm currently (?)
- Nick Treleaven (2/9) Mar 11 2013 The new syntax is fine, and to be preferred.
- Jonathan M Davis (4/15) Mar 11 2013 Both will work, so it's really a matter of personal preference. Quite a ...
- Jesse Phillips (5/12) Mar 11 2013 I just didn't know it made it in yet, so many changes going into
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
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
Jesse Phillips wrote:On Monday, 11 March 2013 at 14:23:41 UTC, Peter Sommerfeld wrote: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.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.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
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, PeterTo 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
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
On 11/03/2013 15:25, Peter Sommerfeld wrote:The new syntax is fine, and to be preferred.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.alias Value = Variant; alias List = Value[]; alias Map = List[Value];Oh, the new alias syntax is in?
Mar 11 2013
On Monday, March 11, 2013 17:22:26 Nick Treleaven wrote:On 11/03/2013 15:25, Peter Sommerfeld wrote:Both will work, so it's really a matter of personal preference. Quite a few people prefer the new syntax though. - Jonathan M DavisThe new syntax is fine, and to be preferred.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.alias Value = Variant; alias List = Value[]; alias Map = List[Value];Oh, the new alias syntax is in?
Mar 11 2013
On Monday, 11 March 2013 at 15:25:55 UTC, Peter Sommerfeld wrote:Jesse Phillips wrote:I just didn't know it made it in yet, so many changes going into release nowadays.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.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