Variant associative arrays

Byron Heads byron.heads at gmail.com
Wed May 8 17:10:48 PDT 2013


I have a variant associative array.  In the example below I am wondering 
if there is a way to create the array without having to indicate the 
variant type on all of the values. Would like to be able to write code 
like #2, or something cleaner/better for #1.  This is intended for a 
library. Other option would be variable parameters, not sure how the 
function signature would look for this.

Any ideas, complaints?

import	std.stdio,
	std.variant;

alias Algebraic!(string, long) HDType;

void main()
{ 

	// #1 This works
	foo(["first" : HDType("John"), 
		"last" : HDType("Doe"), 
		"phone" : HDType(1234546)]);

	// #2 Wont compile, type mismatch string and long
	foo(["first" : "John", 
		"last" : "Doe", 
		"phone" : 1234546]);

	// #3 Other idea
	bar("first", "John", "last", "Doe", "phone", 12345678);	

}

void foo(HDType[string] attr)
{
	foreach(string k, ref HDType v; attr)
	{
		writeln(k, ". ", v.toString());
	}
}


More information about the Digitalmars-d-learn mailing list