Thread overview
String implicit casts
Mar 09, 2006
lightoze
Mar 09, 2006
Sean Kelly
Mar 09, 2006
Derek Parnell
March 09, 2006
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?


March 09, 2006
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
March 09, 2006
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