www.digitalmars.com         C & C++   DMDScript  

D - Why are string literals dual typed?

reply Manfred Nowak <svv1999 hotmail.com> writes:
void f(char[] s){ }
void f(char* s){ }
void main(){
  f("");
}

produces:
function f overloads void(char[]s) and void(char*s) both match argument
list for f

This vanishes if the string literal is cast to one of both.

So why is it this way? And can this dual typedness be achieved for user
generated types as well?

So long!
Mar 25 2004
parent reply "Walter" <walter digitalmars.com> writes:
"Manfred Nowak" <svv1999 hotmail.com> wrote in message
news:c3vluo$2esh$1 digitaldaemon.com...
 void f(char[] s){ }
 void f(char* s){ }
 void main(){
   f("");
 }

 produces:
 function f overloads void(char[]s) and void(char*s) both match argument
 list for f

 This vanishes if the string literal is cast to one of both.

 So why is it this way? And can this dual typedness be achieved for user
 generated types as well?
It's that way because I couldn't justify tying it to one purpose. No. <g>
Mar 25 2004
parent Manfred Nowak <svv1999 hotmail.com> writes:
Walter wrote:

 It's that way because I couldn't justify tying it to one purpose. No. <g>
I do not find this very funny, I find it inconsistent. On one hand if a string literal is used as a parameter of a C function, especially printf, it _must_ be considered as a D-string to avoid an AV, on the other hand if it is used as a parameter of a D function it must be considered _not_ to be a D-string. To install consistent behaviour I see some possibilities: 1) leaving the dual typedness as it is and the need to disambiguate the type of a string literal for D-functions, and therefore disallow the usage of uncasted string literals as parameters of C functions. 2) giving up the dual typedness, and treat string literals as D-strings 3) leaving the dual typedness, but only as a conceptual phrase, by implicit casting to C-strings if used as a parameter to C functions and implicit casting to D-strings in all other casses of usage. So long!
Mar 26 2004