View mode: basic / threaded / horizontal-split · Log in · Help
October 16, 2011
Re: operator "~" does not check type?
On 10/13/2011 01:46 PM, Steven Schveighoffer wrote:
> On Thu, 13 Oct 2011 06:57:09 -0400, Cheng Wei <rivercheng@gmail.com> wrote:
>
>> == Quote from Steven Schveighoffer (schveiguy@yahoo.com)'s article
>>> On Wed, 12 Oct 2011 09:46:57 -0400, Trass3r <un@known.com> wrote:
>>> >> I believe that the primary reasoning for allowing the implicit
>>> >> conversion
>>> >> between int and dchar is so that code like this
>>> >>
>>> >> dchar c = 'a' + 7;
>>> >
>>> > That's a '+' though, not a '~'.
>>> Jonathan meant this better example ;)
>>> string s = "hello";
>>> s ~= 'a' + 7;
>>
>> It's still fine if '~' does not allow implicit casting but '+' does.
>> 'a' + 7 -> 'h' which is already a dchar. So it can be appended to s
>> without casting.
>
> A + B where the types of A and B are integral goes through integer
> promotion rules, inherited from C. Like them or not, they are very
> unlikely to change. This means dchar + int promotes to int, not dchar.

Actually uint afaik.

>
> I think requiring a cast to go from int to dchar would be fine. It's not
> a very common operation, and it clearly causes novice issues.
>

Another argument for requiring an explicit cast is that not every int 
can be converted to a valid dchar.
October 17, 2011
Re: operator "~" does not check type?
On Sun, 16 Oct 2011 18:24:26 -0400, Timon Gehr <timon.gehr@gmx.ch> wrote:

> On 10/13/2011 01:46 PM, Steven Schveighoffer wrote:
>> On Thu, 13 Oct 2011 06:57:09 -0400, Cheng Wei <rivercheng@gmail.com>  
>> wrote:
>>
>>> == Quote from Steven Schveighoffer (schveiguy@yahoo.com)'s article
>>>> On Wed, 12 Oct 2011 09:46:57 -0400, Trass3r <un@known.com> wrote:
>>>> >> I believe that the primary reasoning for allowing the implicit
>>>> >> conversion
>>>> >> between int and dchar is so that code like this
>>>> >>
>>>> >> dchar c = 'a' + 7;
>>>> >
>>>> > That's a '+' though, not a '~'.
>>>> Jonathan meant this better example ;)
>>>> string s = "hello";
>>>> s ~= 'a' + 7;
>>>
>>> It's still fine if '~' does not allow implicit casting but '+' does.
>>> 'a' + 7 -> 'h' which is already a dchar. So it can be appended to s
>>> without casting.
>>
>> A + B where the types of A and B are integral goes through integer
>> promotion rules, inherited from C. Like them or not, they are very
>> unlikely to change. This means dchar + int promotes to int, not dchar.
>
> Actually uint afaik.

Hm... int can hold all dchar values, so I'd expect it to promote to int  
before uint.

testing:

void main()
{
    dchar d;
    auto x = d + 5;
    pragma(msg, typeof(x).stringof);
}

outputs: uint

so you are right!  Seems like an oversight...

-Steve
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home