www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Associative Array Initalizers - Possible Syntax?

reply Xinok <xnknet gmail.com> writes:
I've tried using this syntax as a suggestion before for initalizing associative
arrays. It didn't catch on because a few people thought 'it was too much to
type', and I agree.

int[int] arr = [[0] = 15, [1] = 30, [2] = 45];

Yesterday, I saw an example of Lisp, which gave me an idea to make initalizing
associative arrays quicker using a similar syntax:

int[int] arr = [[0, 1, 2] = [15, 30, 45]];

Initalizing multiple elements in a single expression is much quicker than
initalizing single elements. The syntax could allow you to initalize in groups
of 5 or however many, so it's easy to tell which initalizer belongs to which
element:

char[][int] arr = [[10, 20, 30, 40, 50] = ["Ten", "Twenty", "Thirty", "Forty",
"Fifty"],
                   [60, 70, 80, 90, 100] = ["Sixty", "Seventy", "Eighty",
"Ninety", "One Hundred"]];


This syntax is also nestable:
int[int][int] arr = [[0, 10, 20] = [[5] = 0, [15] = 10, [25] = 20]];


It might be easier to read if braces were used { } for the index list though:
int[int][int] arr = [{0, 10, 20} = [{5} = 0, {15} = 10, {25} = 20]];



And yes, I've already seen the associative array initalizer using mixins...
Feb 12 2007
parent reply Nicolai Waniek <no.spam thank.you> writes:
Hi!

Xinok wrote:
 
 
 It might be easier to read if braces were used { } for the index list though:
 int[int][int] arr = [{0, 10, 20} = [{5} = 0, {15} = 10, {25} = 20]];
 

I don't like each version you provided. The most readable and D-ish (in comparison to static initialization) would be something like this example: char[][][char[]] languageTokens = [ "comments" : ["\\*", "*\\", "//", "\\+", "+\\], "type" : ["bool", "int", "double", "float"] ]; and so on.
 And yes, I've already seen the associative array initalizer using mixins...

I don't know mixins because I don't like mixins (as well as I don't really like templates). Nicolai
Feb 12 2007
parent reply Xinok <xnknet gmail.com> writes:
Nicolai Waniek Wrote:

 I don't like each version you provided. The most readable and D-ish (in
 comparison to static initialization) would be something like this example:
 
 char[][][char[]] languageTokens =
     [
       "comments" : ["\\*", "*\\", "//", "\\+", "+\\],
       "type"     : ["bool", "int", "double", "float"]
     ];
 
 and so on.

The one problem with that example is expressions which would make use of conditionals. Technically, it could work because conditionals have a fixed number of arguments, but it would be hard to read if both sides used conditionals. int[int] arr = [a > b ? a : b : c > d ? c : d]; You could argue that you could use parenthesis. Well, then it just essentially becomes my syntax, except using parenthesis instead of square brackets []. int[int] arr = [(a > b ? a : b) : c > d ? c : d]; Compare against: int[int] arr = [[a > b ? a : b] = c > d ? c : d];
Feb 12 2007
next sibling parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Xinok wrote:
 Nicolai Waniek Wrote:
 
 
I don't like each version you provided. The most readable and D-ish (in
comparison to static initialization) would be something like this example:

char[][][char[]] languageTokens =
    [
      "comments" : ["\\*", "*\\", "//", "\\+", "+\\],
      "type"     : ["bool", "int", "double", "float"]
    ];

and so on.

The one problem with that example is expressions which would make use of conditionals. Technically, it could work because conditionals have a fixed number of arguments, but it would be hard to read if both sides used conditionals. int[int] arr = [a > b ? a : b : c > d ? c : d]; You could argue that you could use parenthesis. Well, then it just essentially becomes my syntax, except using parenthesis instead of square brackets []. int[int] arr = [(a > b ? a : b) : c > d ? c : d]; Compare against: int[int] arr = [[a > b ? a : b] = c > d ? c : d];

I do not find this a compelling reason to require brackets or parentheses in all cases. I do like Nicolai's suggestion, though. (It's very much like Python's syntax, and I believe has been suggested before.) -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Feb 12 2007
next sibling parent cracki <christoph.rackwitz gmail.com> writes:
Kirk McDonald Wrote:

 Xinok wrote:
 Nicolai Waniek Wrote:
 
 
I don't like each version you provided. The most readable and D-ish (in
comparison to static initialization) would be something like this example:

char[][][char[]] languageTokens =
    [
      "comments" : ["\\*", "*\\", "//", "\\+", "+\\],
      "type"     : ["bool", "int", "double", "float"]
    ];

and so on.

The one problem with that example is expressions which would make use of conditionals. Technically, it could work because conditionals have a fixed number of arguments, but it would be hard to read if both sides used conditionals. int[int] arr = [a > b ? a : b : c > d ? c : d]; You could argue that you could use parenthesis. Well, then it just essentially becomes my syntax, except using parenthesis instead of square brackets []. int[int] arr = [(a > b ? a : b) : c > d ? c : d]; Compare against: int[int] arr = [[a > b ? a : b] = c > d ? c : d];

I do not find this a compelling reason to require brackets or parentheses in all cases. I do like Nicolai's suggestion, though. (It's very much like Python's syntax, and I believe has been suggested before.) -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org

Feb 12 2007
prev sibling parent reply cracki <christoph.rackwitz gmail.com> writes:
Kirk McDonald Wrote:
 I do not find this a compelling reason to require brackets or 
 parentheses in all cases. I do like Nicolai's suggestion, though. (It's 
 very much like Python's syntax, and I believe has been suggested before.)

I agree. Requiring brackets makes not much sense. This is not lisp. We have operator precedence and we can make use of it. And if that fails, use parens. No big deal. I also prefer the "[...]" style to the python/ruby/JSON "{...}" style. It's consistent with nromal array literals and spares the curlies from getting overloaded yet another time.
Feb 12 2007
parent reply renoX <renosky free.fr> writes:
cracki a écrit :
 Kirk McDonald Wrote:
 I do not find this a compelling reason to require brackets or 
 parentheses in all cases. I do like Nicolai's suggestion, though.
 (It's very much like Python's syntax, and I believe has been
 suggested before.)

I agree. Requiring brackets makes not much sense. This is not lisp. We have operator precedence and we can make use of it. And if that fails, use parens. No big deal. I also prefer the "[...]" style to the python/ruby/JSON "{...}" style. It's consistent with nromal array literals and spares the curlies from getting overloaded yet another time.

Agreed, if ':' conflicts too much with '? :' (and I don't think it does, parenthesis are here to avoid the problem) then let's use '->' or ':>' instead of ':', not a big deal. Otherwise don't use any special syntax, after all, an associative array is a just list of key,value pair so int tab[char[]] = [["key", 10], ["key2", 11]]; But I really dislike the idea of separating keys and values, sure sometimes this is terser but it's also much more likely to induce hard to find errors. renoX
Feb 13 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
renoX wrote:
 cracki a écrit :
 Kirk McDonald Wrote:
 I do not find this a compelling reason to require brackets or 
 parentheses in all cases. I do like Nicolai's suggestion, though.
 (It's very much like Python's syntax, and I believe has been
 suggested before.)

I agree. Requiring brackets makes not much sense. This is not lisp. We have operator precedence and we can make use of it. And if that fails, use parens. No big deal. I also prefer the "[...]" style to the python/ruby/JSON "{...}" style. It's consistent with nromal array literals and spares the curlies from getting overloaded yet another time.

Agreed, if ':' conflicts too much with '? :' (and I don't think it does, parenthesis are here to avoid the problem) then let's use '->' or ':>' instead of ':', not a big deal. Otherwise don't use any special syntax, after all, an associative array is a just list of key,value pair so int tab[char[]] = [["key", 10], ["key2", 11]];

Except ["key",10] is currently invalid since it's two different types in a list. You could make it work with Tuples though. But a syntax nicer than Tuple!("key", 10) would be cool. --bb
Feb 13 2007
parent Derek Parnell <derek nomail.afraid.org> writes:
I find this syntax reasonably unambiguous...

int[char[]] tab = [ "key" = 10, "key2" = 11 ];

bool[char] xyz = [ 'a' = true, 'b' = false, 'c' = false ];

double[int] qwerty = [ 23 = 17.24, 993 = 0.112 ] ;

char[][char[]] keyval = [ "lol" = "laugh out loud", 
                          "rtfm" = "read the manual",
                          "brb" = "be right back"
                        ]; 

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
14/02/2007 11:48:02 AM
Feb 13 2007
prev sibling parent Xinok <xnknet gmail.com> writes:
 int tab[char[]] = [["key", 10], ["key2", 11]];

I actually like this syntax. IMO it has one problem though, too much use of square brackets [ ] which makes it difficult to read and write if initializing a multi-dimensional array. int arr[char[]][char[]] = [ ["Ten", [["One", 11], ["Two", 12], ["Three", 13]]], ["Twenty", [["One", 21], ["Two", 22], "Three", 23]]], ["Thirty", [["One", 31], ["Two", 32], "Three", 33]]] ]; As you may see, that's very hard to read, and this is only a 2D array. Imagine a 3D or 4D array... Wasn't easy to write either, I had to check my code very carefully to make sure I didn't make any mistakes. To be honest, I think making a unique token is the best solution. int arr[char[]][char[]] = [ "Ten" : ["One" : 11, "Two" : 12, "Three" : 13], "Twenty" : ["One" : 21, "Two" : 22, "Three" : 23], "Thirty" : ["One" : 31, "Two" : 32, "Three" : 33] ]; This is much easier to read, and definitely easier to write. I didn't have to double check my code, and it's just overall quicker. Just a random idea, please don't ridicule me for this one as I'm sure it's a stupid idea anyways, using as a token: int arr[char[]][char[]] = [ [11 "One", 12 "Two", 13 "Three"] "Ten", [21 "One", 22 "Two", 23 "Three"] "Twenty", [31 "One", 32 "Two", 33 "Three"] "Thirty", ]; renoX wrote:
 cracki a écrit :
 Kirk McDonald Wrote:
 I do not find this a compelling reason to require brackets or 
 parentheses in all cases. I do like Nicolai's suggestion, though.
 (It's very much like Python's syntax, and I believe has been
 suggested before.)

I agree. Requiring brackets makes not much sense. This is not lisp. We have operator precedence and we can make use of it. And if that fails, use parens. No big deal. I also prefer the "[...]" style to the python/ruby/JSON "{...}" style. It's consistent with nromal array literals and spares the curlies from getting overloaded yet another time.

Agreed, if ':' conflicts too much with '? :' (and I don't think it does, parenthesis are here to avoid the problem) then let's use '->' or ':>' instead of ':', not a big deal. Otherwise don't use any special syntax, after all, an associative array is a just list of key,value pair so int tab[char[]] = [["key", 10], ["key2", 11]]; But I really dislike the idea of separating keys and values, sure sometimes this is terser but it's also much more likely to induce hard to find errors. renoX

Feb 13 2007
prev sibling parent S. <S s.com> writes:
Xinok Wrote:

 Nicolai Waniek Wrote:
 
 I don't like each version you provided. The most readable and D-ish (in
 comparison to static initialization) would be something like this example:
 
 char[][][char[]] languageTokens =
     [
       "comments" : ["\\*", "*\\", "//", "\\+", "+\\],
       "type"     : ["bool", "int", "double", "float"]
     ];
 
 and so on.

The one problem with that example is expressions which would make use of conditionals. Technically, it could work because conditionals have a fixed number of arguments, but it would be hard to read if both sides used conditionals. int[int] arr = [a > b ? a : b : c > d ? c : d];

Except that static initializers can't use conditionals. Though it seems that D has extended the syntax of initializers to runtime status now versus what was allowed in C and C++.
 
 You could argue that you could use parenthesis. Well, then it just essentially
becomes my syntax, except using parenthesis instead of square brackets [].

Your syntax is confusing and makes it look like you want to have an array of one element set to something else. The syntax of ["element": foo] would jive with struct initializers, indexed array initializers, and was already in the language specification at one point in time. However, I cannot find it now.
Feb 12 2007