Jump to page: 1 2 3
Thread overview
Associative Array Literal Syntax
Jun 25, 2003
Benji Smith
Jun 25, 2003
Georg Wrede
Jun 25, 2003
Ilya Minkov
Jun 25, 2003
Georg Wrede
Jun 25, 2003
Ilya Minkov
Jun 25, 2003
Georg Wrede
Jun 25, 2003
Fabian Giesen
Jun 25, 2003
Georg Wrede
Jun 26, 2003
Helmut Leitner
Jun 26, 2003
BenjiSmith
Jun 26, 2003
Fabian Giesen
Jun 26, 2003
Georg Wrede
Jun 27, 2003
Richard Krehbiel
Jun 27, 2003
Fabian Giesen
Jun 27, 2003
Richard Krehbiel
Jun 27, 2003
Fabian Giesen
Aug 09, 2003
Walter
Aug 09, 2003
Charles Sanders
Aug 09, 2003
Mike Wynn
Aug 10, 2003
Walter
Aug 10, 2003
John Reimer
June 25, 2003
I may be missing something, but I haven't found any information in the D documentation for how to declare associative array literal values all at once.

For example, once I've declared an array as:

char[char[]][] myArray;

I can now only add values to that array one at a time, as follows:

myArray["red"] = "ff0000";
myArray["green"] = "00ff00";
myArray["green"] = "0000ff";

What I'd like to be able to do (especially in cases where I have lots of values to add to the array) would look something like this:

myArray = {

"red" => "ff0000",
"green" => "00ff00",
"blue" => "0000ff"

};

..or something like that. I don't really care about the {} braces or the => operator, but I would like something that lets me declare an associative array literal, just like I can declare a static array using the code:

int[] def = { 1, 2, 3 };


June 25, 2003
>What I'd like to be able to do (especially in cases
>where I have lots of values to add to the array)
>would look something like this:
>
>myArray = {
>
>"red" => "ff0000",
>"green" => "00ff00",
>"blue" => "0000ff"
>
>};

This is entirely off hand, so it may clash with a number
of things, but the above made me think about

myArray = {
"red", "ff0000";
"green", "00ff00";
"blue", "0000ff";
}

Of course, old programmers would in any case write this as

red     ff0000
green   00ff00
blue    0000ff

and then make a disposable macro to format the lines to whatever is needed. The longer the list, the less the particular format matters.

But there's the issue of clarity and readability, and
also safety of hand editing the list afterwards, which
speak for (some) simplification of syntax here.



June 25, 2003
Georg Wrede wrote:
> This is entirely off hand, so it may clash with a number
> of things, but the above made me think about
> 
> myArray = {
> "red", "ff0000";
> "green", "00ff00";
> "blue", "0000ff";
> }

YUCK!
why not simple assignment operator?

myArray = {"red" = "ff0000", 	// ...

This kind of syntax could be very handy in conjuction with a specially constructed prinf with named arguments. :) Actually good for multi-lingual contexts and other similar problems. And again: it should be possibe to define them in-line.

-i.

June 25, 2003
In article <bdcte8$2oig$1@digitaldaemon.com>, Ilya Minkov says...
>YUCK!
>why not simple assignment operator?
>
>myArray = {"red" = "ff0000", 	// ...

I love you, Ilya! You catalyse me to think even further:

myArray = { "red" "ff0000", "green" "00ff00" ...}

or

myArray = {
"red" "ff0000",
"green" "00ff00",
..
}


June 25, 2003
Georg Wrede wrote:
> I love you, Ilya! You catalyse me to think even further:

It's not a very important thinking direction. Walter has some experience, and i believe he would figure out very fast what syntax would appeal to most people in this case, which seems rather simple.

What is way more important, is to convince him that it makes sense. Just as the in-line array definition, which is not implemented yet, and the absence of a suitable way to express a single value as a dynamic array literal.

It may also be that such things are better postponed to a later version (like after 1.0), since really due, announced and already in-spec features are not implemented yet, e.g. getters and setters for Delphi-like class propertes. I believe the spec mentions something of him not having yet figured out a good way to do something (don't really remember what) and it may make sense to help him with ideas in this direction.

-i.

June 25, 2003
> I love you, Ilya! You catalyse me to think even further:
>
> myArray = { "red" "ff0000", "green" "00ff00" ...}
>
> or
>
> myArray = {
> "red" "ff0000",
> "green" "00ff00",
> ..
> }

That is not only quite conterintuitive and nonobvious (IMHO), it also cannot work with current lexical analysis rules:

"Adjacent strings are concatenated with the ~ operator, or by simple
juxtaposition"
("Lexical" part of the D language spec)

Which means that in your case, the lexical analyzer will collapse that list into "redff0000","green00ff00" before the parser even sees it.

-fg


June 25, 2003
In article <bdd087$2rl1$1@digitaldaemon.com>, Ilya Minkov

>What is way more important, is to convince him that it makes sense. Just as the in-line array definition, which is not implemented yet, and the absence of a suitable way to express a single value as a dynamic array literal.

Hmm. I think, rather than trying to convince Walter, we should just discuss the issues. "With enough eyes" these issues tend to become clarified. At that point we all see the light (which may even shine from a direction none of us thought initially), and by that time Walter may have decided to implement the thing, before we get around to asking him. (Which of course, is not the same thing as it appearing in the very next version of D!)

>It may also be that such things are better postponed to a later version (like after 1.0), since really due, announced and already in-spec features are not implemented yet, e.g. getters and setters for Delphi-like class propertes.

Yes, it is all too easy for us to gallop away conjuring up creative ideas faster than anyone could ever implement them.

Recently there have been a few ideas about tidying up the syntax of D, and I must say I am happy with them. I think there are few things more important than the smoothness and the coherence of the syntax of a new language. What comes second is the smoothness and the coherence of the semantics.

Then, after a space, come all the other things.

I think Walter has done an excellent job so far! (And maybe I am missing other persons who really deserve credit, too.)

Right now, we are in such an early stage that we can afford a few ventures into unknown or dubious territory with both of them, but we have to keep in mind that whatever we do as trial-and-error, has to be diligently pruned, before it sticks.

There are enough grave examples of leaving these in the language "for just a few minutes too long, and then they stuck!" (Just read some of the later writings of Stroustrup, and maybe Kernighan or Ritchie.)




June 25, 2003
In article <bdd2h5$2tt8$1@digitaldaemon.com>, Fabian Giesen says...
>
>> I love you, Ilya! You catalyse me to think even further:
..
>> myArray = { "red" "ff0000", "green" "00ff00", ..  }
>
>That is not only quite conterintuitive and nonobvious (IMHO), it also cannot work with current lexical analysis rules:

Hmm. Now I have a choice between saying "I hate you, Ilya!", or reminding everyone of the disclaimer I wrote 2 messages ago.   :-)

Anyhow, that is a valid point!



June 26, 2003
> why not simple assignment operator?
>
> myArray = {"red" = "ff0000", // ...


Since structs are initialized with the ":" operator associative arrays should to

char[char[]] myArray = [
    "red": "ff0000",
    "green": "00ff00",
    ...
]


June 26, 2003
"Julio César Carrascal Urquijo" <adnoctum@phreaker.net> escribió en el
mensaje news:bddhhf$bqj$1@digitaldaemon.com...
| > why not simple assignment operator?
| >
| > myArray = {"red" = "ff0000", // ...
|
|
| Since structs are initialized with the ":" operator associative arrays
| should to
|
| char[char[]] myArray = [
|     "red": "ff0000",
|     "green": "00ff00",
|     ...
| ]
|
|

I agree

————————————————————————— Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.493 / Virus Database: 292 - Release Date: 2003-06-25


« First   ‹ Prev
1 2 3