Length comparison
Lionello Lunesu
lio at lunesu.remove.com
Tue Dec 5 04:51:15 PST 2006
xs0 wrote:
> Bill Baxter wrote:
>> This bit me just now:
>>
>> if (-1 < somearray.length) {
>> }
>>
>> Ouch. Took me a while to figure out that that bit of code was the
>> problem.
>>
>> Is there some way to make these kind of bugs less likely or easier to
>> find?
>
> It should be made illegal :) In particular:
> - comparing a negative constant with an unsigned variable
> - comparing a too large constant with a signed variable (like
> 3_000_000_000 and something of type int)
> - setting an unsigned var to a negative constant (uint a = -1)
>
> For other cases, comparing signed with unsigned should produce a warning
> and, more importantly, work properly:
>
> int a;
> uint b;
>
> a < b should be a < 0 || cast(uint)a < b
> a > b should be a > 0 && cast(uint)a > b
> etc.etc.
http://d.puremagic.com/issues/show_bug.cgi?id=259
Could you please add your remarks to that bugzilla issue as well?
Especially the proper implementation for mixed comparison. I agree that
the result should at least be correct, no matter* how slow.
L.
* of course, within limits :)
More information about the Digitalmars-d
mailing list