First Impressions

Lionello Lunesu lio at lunesu.remove.com
Fri Sep 29 06:51:49 PDT 2006


Anders F Björklund wrote:
> Lionello Lunesu wrote:
> 
>> Perhaps, using string instead of char[], it's more obvious that it's 
>> not zero-terminated. I've seen D examples online that just cast a 
>> char[] to char* for use in MessageBox and the like (which worked since 
>> it were string constants.)
> 
> And probably only for ASCII string constants, at that...

Right, that too!

char[] somestring = "....";
func( somestring[0] ); // WRONG: somestring[x] is not 1 character!

Using "string" would make it less obvious:

string somestring = ".....";
func( somestring[0] ); // [0] means what?

This goes for iteration as well. DMD will still deduct 'char' as the 
type type, but at least one's less likely to type foreach(char c;str).

If you want to iterate the UNICODE characters in a string, you'll 
specify "dchar" as the type and you won't worry about "how come I can 
use dchar when it's a char[]":

foreach(dchar c; somestring)
   func(c); // correct


L.



More information about the Digitalmars-d mailing list