digitalmars.D.learn - Variant associative arrays
- Byron Heads (30/30) May 08 2013 I have a variant associative array. In the example below I am wondering...
- bearophile (7/23) May 08 2013 If your purpose is to write such associative literal without
- Byron Heads (2/24) May 09 2013 That approch may work. Will have to experiment with the API more.
- evilrat (32/67) May 08 2013 first doesn't compile with DMD 2.062 as int implicitly not
- Byron Heads (7/48) May 09 2013 ':' : '\n')
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 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() { foo(["first" : HDType("John"), "last" : HDType("Doe"), "phone" : HDType(1234546)]); foo(["first" : "John", "last" : "Doe", "phone" : 1234546]); bar("first", "John", "last", "Doe", "phone", 12345678); } void foo(HDType[string] attr) { foreach(string k, ref HDType v; attr) { writeln(k, ". ", v.toString()); } }
May 08 2013
Byron Heads: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 ... foo(["first" : HDType("John"), "last" : HDType("Doe"), "phone" : HDType(1234546)]); foo(["first" : "John", "last" : "Doe", "phone" : 1234546]);If your purpose is to write such associative literal without repeating the HDType(), then a possibility is to write the data in a single string, and feed it to a compile-time function that splits it and builds the associative array. Bye, bearophile
May 08 2013
On Thu, 09 May 2013 02:33:08 +0200, bearophile wrote:Byron Heads:That approch may work. Will have to experiment with the API more.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 ... "last" : HDType("Doe"), "phone" : HDType(1234546)]); "John", "last" : "Doe", "phone" : 1234546]);If your purpose is to write such associative literal without repeating the HDType(), then a possibility is to write the data in a single string, and feed it to a compile-time function that splits it and builds the associative array. Bye, bearophile
May 09 2013
On Thursday, 9 May 2013 at 00:10:48 UTC, Byron Heads wrote: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 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() { foo(["first" : HDType("John"), "last" : HDType("Doe"), "phone" : HDType(1234546)]); foo(["first" : "John", "last" : "Doe", "phone" : 1234546]); bar("first", "John", "last", "Doe", "phone", 12345678); } void foo(HDType[string] attr) { foreach(string k, ref HDType v; attr) { writeln(k, ". ", v.toString()); } }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"); } }
May 08 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.
May 09 2013