DMD 1.014 release

Don Clugston dac at nospam.com.au
Fri Apr 27 03:16:31 PDT 2007


> Frits van Bommel Wrote:
> 
>> bobef wrote:
>>> this class member:
>>>
>>> static auto allsupportedfiles2=["avi":1,"mpg":1,"mpeg":1,"asf":1,"asx":1,"dat":1,"vob":1,"ifo":1,"wma":1,"wmv":1,"mov":1,"qt":1,"mp4":1,"fli":1,"rm":1,"nuv":1,"ogg":1,"ogm":1,"sdp":1,"pva":1,"roq":1,"flm":1,"cpk":1,"vivo":1,"mkv":1,"mp3":1,"mpe":1,"divx":1,"mpv":1,"m1v":1,"m2v":1,"dv":1,"m4v":1,"264":1,"mp1":1,"mp2":1,"mpa":1,"ac3":1,"m4a":1,"26l":1,"jsv":1,"wav":1/*,"rpl"*/,"flv":1];
>>>
>>> app.d(126): Error: cannot infer type from this array initializer
>>>
>>> static bool[char[]] allsupportedfiles2=["avi":1,"mpg":1,"mpeg":1,"asf":1,"asx":1,"dat":1,"vob":1,"ifo":1,"wma":1,"wmv":1,"mov":1,"qt":1,"mp4":1,"fli":1,"rm":1,"nuv":1,"ogg":1,"ogm":1,"sdp":1,"pva":1,"roq":1,"flm":1,"cpk":1,"vivo":1,"mkv":1,"mp3":1,"mpe":1,"divx":1,"mpv":1,"m1v":1,"m2v":1,"dv":1,"m4v":1,"264":1,"mp1":1,"mp2":1,"mpa":1,"ac3":1,"m4a":1,"26l":1,"jsv":1,"wav":1/*,"rpl"*/,"flv":1];
>>>
>>> app.d(126): Error: cannot use array to initialize bool[char[]]
>>>
>>>
>>> why?
>> I guess that's what this sentence in the docs means:
>> =====
>> An AssocArrayLiteral cannot be used to statically initialize anything.
>> =====
>> (Note: The docs aren't up on the site yet, but you can find this in 
>> dmd/html/d/expression.html#AssocArrayLiteral)
>>
>> The error message could be clearer.


In practice, you _can_ have initialized static AAs, provided they are 
read-only (which is probably the main time you want to supply 
initializers). The declaration syntax for a read-only AA slightly lacks 
syntactic sugar, but the syntax for usage is perfect:

char[][uint] symTable() { return [2u:"he",4:"ho",6:"hi"]; }

void main()
{
     for (int k=1; k<=6; ++k) {
         if ((k&1)==0) printf("%.*s\n", symTable[k]);
     }
}

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.



More information about the Digitalmars-d-announce mailing list