www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - char[], wchar[], and dchar[] string literals.

reply Regan Heath <regan netwin.co.nz> writes:
http://www.digitalmars.com/d/arrays.html mentions..

<quote>
The type of a string is determined by the semantic phase of compilation. 
The type is one of: char[], wchar[], dchar[], and is determined by 
implicit conversion rules. If there are two equally applicable implicit 
conversions, the result is an error. To disambiguate these cases, a cast 
is appropriate:

(wchar [])"abc"// this is an array of wchar characters

String literals are implicitly converted between chars, wchars, and dchars 
as necessary.
</quote>

The cast above is old style and needs updating, I went looking for the 
Wiki for this and could not find it?

Anyway..

This code:
dchar[] foo(dchar[] name)
{
   dchar[] testString = "<" ~ name ~ ">";
   return testString;
}

fails with the error:
   incompatible types for (("<") ~ (name)): 'char[]' and 'dchar[]'

Yet this code:
dchar[] foo(dchar[] name)
{
   dchar[] testString = "<";
   testString ~= name;
   testString ~= ">";
   return testString;
}

compiles/works fine.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 11 2004
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
 The cast above is old style and needs updating

which you write as cast(wchar[])string;.
 dchar[] foo(dchar[] name)
 {
    dchar[] testString = "<" ~ name ~ ">";
    return testString;
 }

for some reason, the compiler is not smart enough to know that those string literals are dchar. so you must tell it: dchar[] foo(dchar[] name) { dchar[] testString = cast(dchar[])"<" ~ name ~ cast(dchar[])">"; return testString; } compiles and works correctly. what is everyone's preoccupation with UTF anyway? why can't we all get along and use ASCII ;)
Jul 12 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Mon, 12 Jul 2004 11:01:50 -0400, Jarrett Billingsley 
<kb3ctd2 yahoo.com> wrote:
 The cast above is old style and needs updating

which you write as cast(wchar[])string;.

Yep. :)
 dchar[] foo(dchar[] name)
 {
    dchar[] testString = "<" ~ name ~ ">";
    return testString;
 }

for some reason, the compiler is not smart enough to know that those string literals are dchar. so you must tell it:

I know. I believe the documentation implies that it should be smart enough to tell, so I think it is a bug.
 dchar[] foo(dchar[] name)
 {
    dchar[] testString = cast(dchar[])"<" ~ name ~ cast(dchar[])">";
    return testString;
 }

 compiles and works correctly.

Thanks for this. I worked it out myself. I was just posting the bug as I see it.
 what is everyone's preoccupation with UTF anyway?  why can't we all get
 along and use ASCII ;)

:) yeah.. I only *found* this cos a friend of mine was trying D by writing an XML library, which according to the spec must support the various flavours of UTF. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 12 2004