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

Nick Sabalausky a at a.a
Tue May 17 01:22:07 PDT 2011


"Walter Bright" <newshound2 at digitalmars.com> wrote in message 
news:iqsu2b$1aqs$1 at digitalmars.com...
> On 5/16/2011 9:09 PM, Jonathan M Davis wrote:
>> On 2011-05-16 21:04, Don wrote:
>>> It's not easy. Simply disallowing it would make code like this illegal:
>>>
>>> char c = '0' + n;
>>
>> Well, forcing the n to be cast to char or byte would do it, but that _is_ 
>> kind
>> of ugly.
>>
>> char c = '0' + cast(char)n;
>>
>> Definitely a good point though.
>
> It's THE reason why such casts are not allowed. Pascal requires such 
> casts, and I always hated it for that.
>
> The casts are not only annoying, they can cause bugs when the types of the 
> rest of the expression are changed in maintenance.
>
> There isn't any simple answer to mixed integer type arithmetic, although 
> there are many "obvious" answers that have subtly pernicious behavior.
>

I was going to suggest the rule:

"char + numeric type == char" (and likewise for wchar/dchar), provided that 
the value range propogation works out, and if it doesn't then you do: char c 
= cast(char)('0' + n)

Is that one of the "obvious" answers that has subtly pernicious behavior? If 
so, I'm curious what the problem with it is.





More information about the Digitalmars-d mailing list