Associative Array Initalizers - Possible Syntax?

S. S at s.com
Mon Feb 12 14:34:47 PST 2007


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.



More information about the Digitalmars-d mailing list