size_t index=-1;

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Mar 18 02:48:54 PDT 2016


On Thursday, 17 March 2016 at 17:09:46 UTC, Steven Schveighoffer 
wrote:
> On 3/16/16 6:37 PM, Mathias Lang wrote:
>> On Wednesday, 16 March 2016 at 21:49:05 UTC, Steven 
>> Schveighoffer wrote:
>>> No, please don't. Assigning a signed value to an unsigned 
>>> (and vice
>>> versa) is very useful, and there is no good reason to break 
>>> this.
>>
>> I'm not talking about removing it completely. The implicit 
>> conversion
>> should only happen when it's safe:
>>
>> ```
>> int s;
>> if (s >= 0) // VRP saves the day
>> {
>>    uint u = s;
>> }
>> ```
>>
>> ```
>> uint u;
>>
>> if (u > short.max)
>>    throw new Exception("Argument out of range");
>> // Or `assert`
>> short s = u;
>> ```
>
> Converting unsigned to signed or vice versa (of the same size 
> type) is safe. No information is lost.

Strictly speaking yes, but typically, an `int` isn't used as a 
bit-pattern but as an integer (it's in the name). Such behaviour 
is very undesirable for integers.

> It's the comparison between the two which confuses the heck out 
> of people. I think we can solve 80% of the problems by just 
> fixing that.

That's probably true, anyway.



More information about the Digitalmars-d-learn mailing list