should int/short/byte implicitly cast to dchar/wchar/char?

Steven Schveighoffer schveiguy at yahoo.com
Tue May 17 05:58:34 PDT 2011


On Tue, 17 May 2011 00:04:07 -0400, Don <nospam at nospam.com> wrote:

> Nick Sabalausky wrote:
>> "Steven Schveighoffer" <schveiguy at yahoo.com> wrote in message  
>> news:op.vvk48tn9eav7ka at localhost.localdomain...
>> (int/short/byte can sometimes implicitly cast to dchar/wchar/char)
>>> What do you think?
>>>
>>  Ick! Kill it!
>>
>
> It's not easy. Simply disallowing it would make code like this illegal:
>
> char c = '0' + n;
>
>

don't you mean dchar c = '0' + n?

And I wouldn't mind if dchar + int => dchar.  I also wouldn't mind  
requiring a cast when range propagation proves the dchar could be invalid.

In any case, I think:

string s = "hello";
s ~= 123;

looks like it should result in:

assert(s == "hello123");

Especially anyone coming from any other language.  They certainly would  
not expect it to be "hello{"

To me, dchar is the more specialized type, it's like a typedef of uint --  
you should be able to implicitly cast to uint/int, but not to dchar.

It's kind of like a pointer, you can do this:
int n;
int* ptr;
ptr += n;

but you can't do this:

ptr = n;

-Steve


More information about the Digitalmars-d mailing list