Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
March 11, 2013 Variant confusion | ||||
---|---|---|---|---|
| ||||
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 |
March 11, 2013 Re: Variant confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Sommerfeld | 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.
|
March 11, 2013 Re: Variant confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | 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 |
March 11, 2013 Re: Variant confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Sommerfeld | 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.
|
March 11, 2013 Re: Variant confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Sommerfeld | 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.
|
March 11, 2013 Re: Variant confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Treleaven | 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
|
March 11, 2013 Re: Variant confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | 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
|
March 12, 2013 Re: Variant confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Sommerfeld | 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. |
Copyright © 1999-2021 by the D Language Foundation