Nesting Variants

Wyatt wyatt.epp at gmail.com
Tue May 21 18:04:34 PDT 2013


On Monday, 20 May 2013 at 08:55:24 UTC, evilrat wrote:
> yes, you forgot to take exact value, it doesn't know anything 
> about array you put it in, so if you take that array explicitly 
> and put value on array element it would work
>
> ender[0] = one;
> ender[0].get!(Variant[])[0] = key;
> writeln(ender[0][0]); // writes 1
>
> also you can check whats inside by doing just this 
> "writeln(ender);" in case you are not sure what's going on.

I was trying to do this as a way to obtain a concise syntax for 
manipulating a tree of elements deserialized at runtime, ex:
data["foo"]["bar"] = "baz";
assert( data["foo"]["bar"] == "baz" );
foreach( subtree; data["foo"] )
     doSubtreeOp( subtree );

Having to place .get!(Variant[]) between each dereference will 
defeat my use case.

Associative arrays fail in the same way, btw.  I'm using integers 
in the first example because it should be simpler to get those 
right.

I expected this to work because the value of 'ender[0]' will be a 
variant that contains an array, and such a variant should be able 
to index the contained array because of its opIndex overload.


More information about the Digitalmars-d-learn mailing list