www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Proposal: type suffix for string and character literals

reply Michael Butscher <mbutscher gmx.de> writes:
Hi,

the following code doesn't compile with DMD 0.126:


-----
void pr(char[] s)
{
    printf("char[]\n");
}

void pr(wchar[] s)
{
    printf("wchar[]\n");
}

int main()
{
    pr("xyz");

    return 0;
}
-----


The reason seems to be that the type of a string literal (char[], wchar[] or 
dchar[]) is determined by the type of the variable or function parameter it is 
assigned to. In the code above the compiler can't decide which pr() function 
should be called.

This can be worked around by using a cast, e. g.

  pr(cast(char[])"xyz");

but I think this is very inconvenient.

Therefore I would suggest to introduce the suffixes c, w and d to specify a 
char[], wchar[] or dchar[] array respectively.

The line above could then be written as:

  pr("xyz"c);



Interestingly, character literals have always type char, but it would be
helpful 
to specify their type as well using the same suffixes.



Michael
Jun 17 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Fri, 17 Jun 2005 20:34:22 +0200, Michael Butscher wrote:


[snip]

 Therefore I would suggest to introduce the suffixes c, w and d to specify a 
 char[], wchar[] or dchar[] array respectively.
 
 The line above could then be written as:
 
   pr("xyz"c);

How many times has this been proposed or asked for now? I've lost count. -- Derek Parnell Melbourne, Australia 18/06/2005 7:51:58 AM
Jun 17 2005
parent Chris Sauls <ibisbasenji gmail.com> writes:
Derek Parnell wrote:
 How many times has this been proposed or asked for now? I've lost count.

Too many. Maybe Walter should just go ahead and implement it, then the problem would go away. (Was that subtle? I was going for subtle...) Maybe it could be a prefix rather than a suffix, though. Just because A) the parser already checks for prefixes on string literals: 'r' for alt-syntax WYSIWYG strings, and 'x' for hex-strings; and B) the way the literal is read would likely be slightly different depending on whether it is UTF-8, 16, or 32. # char[] s8 = "foo" ; # wchar[] s16 = w"foo" ; # dchar[] s32 = d"foo" ; # char[] s8r = r"foo" ; // or `foo` # wchar[] s16r = rw"foo" ; // or w`foo` # dchar[] s32r = rd"foo" ; // or d`foo` # char[] s8x = x"60 6E 6E"; # wchar[] s16x = xw"60 6E 6E"; # dchar[] s32x = xd"60 6E 6E"; -- Chris Sauls
Jun 17 2005