Nesting Variants

Wyatt wyatt.epp at gmail.com
Sun May 19 16:31:10 PDT 2013


I'm trying to use Variants and ran into the following sort of 
situation:

//Using DMD 2.062
import std.stdio;
import std.variant;

void main(){
	int key = 1;
	Variant[] one;
	Variant[] ender;
	one = new Variant[](1);
	ender = new Variant[](1);

	//This bails:
	//std.variant.VariantException at std/variant.d(1224): Variant: 
attempting to use incompatible types int and 
std.variant.VariantN!(32LU).VariantN
	ender[0] = one;
	ender[0][0] = key;
	writeln(ender[0][0]);

	//Also bails only rather than int, it's 
std.variant.VariantN!(32LU).VariantN*:	
	//ender[0][0] = new Variant(key);

	//This works fine:
	//one[0] = key;
	//ender[0] = one;
	//writeln(ender[0][0]);
}

The long and short of it seems to be that you can't (easily) 
assign to an element of a Variant array within a Variant array 
but you CAN access it as long as you build the whole thing 
upside-down.  Can anyone shed some light on why this is?  Am I 
just missing some not-completely-obvious step?

Oh, I should probably mention I was originally using associative 
arrays, so I thought maybe I'd hit one of the bugs related to 
that. But as you can see, it's happening even with ordinary 
arrays.


More information about the Digitalmars-d-learn mailing list