digitalmars.D - assoc array initialization like in php
- dennis luehring (26/26) Feb 20 2006 in php you can init an assoc array
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (16/18) Feb 20 2006 It's on the D wishlist, along with array inits...
- Chris Sauls (3/7) Feb 20 2006 I vote for this one. It is consistant with struct initializers, and jus...
- Dawid =?UTF-8?B?Q2nEmcW8YXJraWV3aWN6?= (2/6) Feb 20 2006 +1 looks good
- nick (2/8) Feb 20 2006 You know, this syntax looks almost as good as the proposed ones. Perhaps
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (11/19) Feb 20 2006 Just like in Java, it gets a little uglier when it's a "global":
- Derek Parnell (37/46) Feb 20 2006 You play around with the template syswtem to get something useful ...
in php you can init an assoc array (for example with string-key and numeric value) with $test = array ( 'test' => 1, 'bla' => 2, 'blub' => 3 ); can we have something equal to this in d? int test[char[]] = { "test" => 1, "bla" => 2, "blub" => 3 } or an enum example enum ekey { TEST, BLA, BLUB }; int test[ekey] = { TEST => 1, BLA => 2, BLUB => 3 } or is this an working feature in d (i cant find such examples in the docs) ciao dennis
Feb 20 2006
dennis luehring wrote:in php you can init an assoc array[...]can we have something equal to this in d?It's on the D wishlist, along with array inits... i.e. int test[] = [ 1,2,3 ]; See http://www.digitalmars.com/d/archives/26695.html Choosing a syntax would be a good thing, though ? maybe: int test[char[]] = [ "test": 1, "bla": 2, "blub": 3 ]; --anders PS. For now, you need to use explicit initializers instead: int test[char[]]; test["test"] = 1; test["bla"] = 2; test["blub"] = 3;
Feb 20 2006
Anders F Björklund wrote:Choosing a syntax would be a good thing, though ? maybe: int test[char[]] = [ "test": 1, "bla": 2, "blub": 3 ];I vote for this one. It is consistant with struct initializers, and just pleases me eyes. -- Chris Nicholson-Sauls
Feb 20 2006
Anders F Björklund wrote:Choosing a syntax would be a good thing, though ? maybe: int test[char[]] = [ "test": 1, "bla": 2, "blub": 3 ];+1 looks good
Feb 20 2006
PS. For now, you need to use explicit initializers instead: int test[char[]]; test["test"] = 1; test["bla"] = 2; test["blub"] = 3;You know, this syntax looks almost as good as the proposed ones. Perhaps no change in necessary.
Feb 20 2006
nick wrote:Just like in Java, it gets a little uglier when it's a "global": int test[char[]]; static this() { test["test"] = 1; test["bla"] = 2; test["blub"] = 3; } But having array/map literals, doesn't change this "old" syntax ? --andersFor now, you need to use explicit initializers instead: int test[char[]]; test["test"] = 1; test["bla"] = 2; test["blub"] = 3;You know, this syntax looks almost as good as the proposed ones. Perhaps no change in necessary.
Feb 20 2006
On Mon, 20 Feb 2006 21:35:54 +1100, dennis luehring <dennis_member pathlink.com> wrote:in php you can init an assoc array (for example with string-key and numeric value) with $test = array ( 'test' => 1, 'bla' => 2, 'blub' => 3 );You play around with the template syswtem to get something useful ... // --------------------- import std.c.stdarg; template setter(Tk, Td) { Td[Tk] setter ( ... ) { Td[Tk] lTemp; Tk key; Td data; int ready = 0; for (int i = 0; i < _arguments.length; i+=2) { key = va_arg!(Tk)(_argptr); data = va_arg!(Td)(_argptr); lTemp[ key ] = data; } return lTemp; } } import std.stdio; void main() { auto test = setter!(char[], int)( "test", 1, "bla", 2, "blub", 3 ); foreach(char[] x, int q; test) writefln("Key=%s, Data = %s", x, q); } // --------------------- -- Derek Parnell Melbourne, Australia
Feb 20 2006