Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 12, 2007 Associative Array Initalizers - Possible Syntax? | ||||
---|---|---|---|---|
| ||||
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... |
February 12, 2007 Re: Associative Array Initalizers - Possible Syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Xinok | 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 |
February 12, 2007 Re: Associative Array Initalizers - Possible Syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicolai Waniek | 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];
|
February 12, 2007 Re: Associative Array Initalizers - Possible Syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Xinok | 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 |
February 12, 2007 Re: Associative Array Initalizers - Possible Syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Xinok | 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. |
February 13, 2007 Re: Associative Array Initalizers - Possible Syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kirk McDonald | 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
|
February 13, 2007 Re: Associative Array Initalizers - Possible Syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kirk McDonald | 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.
|
February 13, 2007 Re: Associative Array Initalizers - Possible Syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to cracki | 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
|
February 14, 2007 Re: Associative Array Initalizers - Possible Syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to renoX | 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
|
February 14, 2007 Re: Associative Array Initalizers - Possible Syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter |
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
|
Copyright © 1999-2021 by the D Language Foundation