Wide characters support in D

Simen kjaeraas simen.kjaras at gmail.com
Mon Jun 7 15:46:10 PDT 2010


Ruslan Nikolaev <nruslan_devel at yahoo.com> wrote:

> 1. When we have 2 methods (one with wchar[] and another with char[]),  
> how D will determine which one to use if I pass a string "hello world"?

String literals in D(2) are of type immutable(char)[] (char[] in D1) by
default, and thus will be handled by the char[]-version of the function.
Should you want a string literal of a different type, append a c, w, or
d to specify char[], wchar[] or dchar[]. Or use a cast.

> Since D language is targeted on system programming, why not to try to  
> use whatever works better on a particular system (e.g. char will be 2  
> bytes on Windows and 1 byte on Linux; it can be a compiler switch, and  
> all libraries can be compiled properly on a particular system).

Because this leads to unportable code, that fails in unexpected ways
when moved from one system to another, thus increasing rather than
decreasing the cognitive load on the hapless programmer.

> It's still necessary to have all 3 types of char for cooperation with C.  
> But in those cases byte, short and int will do their work.

Absolutely not. One of the things D tries, is doing strings right. For
that purpose, all 3 types are needed.

> In my opinion, to separate notion of character from byte would be nice,  
> and it makes sense as a particular platform uses either UTF-8 or UTF-16  
> natively. Programmers may write universal code (like TCHAR on Windows).  
> Unfortunately, C uses 'char' and 'byte' interchangeably but why D has to  
> make this mistake again?

D has not. A char is a character, a possibly incomplete UTF-8 codepoint,
while a byte is a byte, a humble number in the order of -128 to +127.

Yes, it is possible to abuse char in D, and byte likewise. D aims to allow
programmers to program close to the metal if the programmer so wishes, and
thus does not pretend char is an opaque type about which nothing can be
known.

-- 
Simen


More information about the Digitalmars-d mailing list