www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - String implicit casts

reply lightoze <lightoze_member pathlink.com> writes:
I use this two functions:

void x(char[] x) {}
void x(wchar[] x) {}

This works:

x(cast(char[])"x");
x(cast(wchar[])"x");

This do not:

x("x");

I have found nothing about it in manual, can anyone tell me if it is normal or
not?
Mar 09 2006
next sibling parent Sean Kelly <sean f4.ca> writes:
lightoze wrote:
 I use this two functions:
 
 void x(char[] x) {}
 void x(wchar[] x) {}
 
 This works:
 
 x(cast(char[])"x");
 x(cast(wchar[])"x");
 
 This do not:
 
 x("x");
 
 I have found nothing about it in manual, can anyone tell me if it is normal or
 not?

It's normal, and is a result of the overloading rules in D. To resolve an overload with string literals, try this: x( "x"c ); // declare "x" as a char string Templates can help as well, as in many cases you don't really need separate overloads for each char type. Sean
Mar 09 2006
prev sibling parent Derek Parnell <derek psych.ward> writes:
On Thu, 9 Mar 2006 22:56:04 +0000 (UTC), lightoze wrote:

 I use this two functions:
 
 void x(char[] x) {}
 void x(wchar[] x) {}
 
 This works:
 
 x(cast(char[])"x");
 x(cast(wchar[])"x");
 
 This do not:
 
 x("x");
 
 I have found nothing about it in manual, can anyone tell me if it is normal or
 not?

It is 'normal' but not expected. Most people assume that an unadorned string literal is a char[] but it turns out that the compiler is a little more discerning. But in general, if the compiler cannot decide which utf character type to encode the literal with, it complains and you have to tell it what to do. Fortunately, we can add a suffix to the literal to tell the compiler what we want instead of the chunky cast syntax. In your case ... x( "x"c ); x( "x"w ); -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 10/03/2006 10:15:09 AM
Mar 09 2006