Variant associative arrays

Byron Heads byron.heads at gmail.com
Thu May 9 11:40:15 PDT 2013


On Thu, 09 May 2013 03:29:06 +0200, evilrat wrote:

> first doesn't compile with DMD 2.062 as int implicitly not converted to
> long.
> 
> foo func takes associative array, within this example you can use type
> Variant[string] to make life a bit easier(but i can't recommend it for
> ur real code cause we don't know the usage).
> 
> and for third func here is example code, though it doesn't takes
> associative array you can rewrite it to suit ur needs, also notice that
> it's a bit dirty and not very readable cause i'm superlazy and quite in
> a hurry, so you can improve it yourself
> 
> ---------------
> 
> void bar(...)
> {
> 	import core.vararg;
> 
> 	enum string handler = `.visit! (
> 		// string type handler (string s) => write(s, pos %2 == 
0 ? ':' :
> 		'\n'),
> 		// long type handler (long l) => write(l, pos %2 == 0 ? 
':' : '\n')
> 		)();`;
> 
> 
> 	for( int pos = 0; pos < _arguments.length ; pos++ )
> 	{
> 
> 		if ( _arguments[pos] == typeid(HDType) )
> 			mixin(`(va_arg!(HDType)(_argptr))` ~ handler );
> 
> 		else if ( _arguments[pos] == typeid(string) )
> 			mixin(`HDType(va_arg!(string)(_argptr))` ~ 
handler );
> 
> 		else if ( _arguments[pos] == typeid(long) )
> 			mixin(`HDType(va_arg!(long)(_argptr))` ~ handler 
);
> 
> 		else
> 			throw new Exception("wrong type");
> 	}
> }



I think I had a large const in there when I tested so it worked then.  
This gives me a few ideas, but I may try a different API.


More information about the Digitalmars-d-learn mailing list