DMD 1.014 release

Georg Wrede georg at nospam.org
Sun May 6 09:45:44 PDT 2007


Don Clugston wrote:
> BTW, these read-only AAs are great for eliminating case statements!
> ------------------
> char [] a;
> switch(k)
> {
> case 2: a="he"; break;
> case 4: a= "ho"; break;
> case 6: a="hum"; break;
> }
> ------------------
> becomes:
> ------------------
> char [] a = [2:"he"[], 4:"ho", 6:"hum"][k];
> ------------------
> Which is pretty awesome when you have a lot of trivial cases; ought to 
> generate better code, too.

This certainly looks like it could become an idiom! In that case I might 
suggest another way of writing it:

   char [] a =
   [
     2:"he"[],
     4:"ho",
     6:"hum"
   ]
   [k];

The latter is easier to read, especially when there are a lot of 
members. Problem with both is still that [k] ends up quite far from the 
assignment. Of course with a named AA we could write:

char [] a = myReadOnlyAA [k];



More information about the Digitalmars-d-announce mailing list