Question about wchar[]

Ali Çehreli acehreli at yahoo.com
Mon Feb 4 10:41:35 PST 2013


On 02/04/2013 10:22 AM, Ali Çehreli wrote:
 > On 02/04/2013 10:13 AM, ollie wrote:
 >  > I am using wchar[] and find the usage clunky. Am I doing something
 > wrong?
 >  >
 >  > Example:
 >  > // Compiler complains that wchar[] != immutable(char)[]
 >  > wchar[] wstr = "This is a wchar[]";
 >
 > There is nothing but a wchar slice that is going to provide access to
 > the chars on the right-hand side. Unfortunately that is not possible
 > because neither the right-hand side has wchars nor they are mutable.

As Steven's post shows, it would work if the left-hand side was immutable:

     immutable(wchar)[] wstr = "This is a wchar[]";

That line involves some help from the compiler: Because the type of the 
literal on the right-hand side is not specified, the compiler makes one 
that matches the type of the left-hand side.

In other words, one should not do what I did and assume that literals 
"like this" are always char arrays. They are char arrays only when there 
is c at the end "like this"c. Otherwise, although they default to char, 
the actual type is decided by the compiler.

Ali



More information about the Digitalmars-d-learn mailing list