size_t index=-1;

Anonymouse via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 16 15:07:39 PDT 2016


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.
>
> -Steve

I agree, but implicitly allowing for comparisons between the two 
allows for easy and *silent* mistakes. Also for comparisons of 
less-than-zero, for those functions we have that return -1 on 
failure.

import std.string : indexOf;
size_t pos = "banana".indexOf("c");
if (pos > 0) {
   // oops
}

The above is naturally a programmer error but it's not something 
that will net you an immediate crash. It will just silently not 
behave as you meant, and you'll find yourself with a lot of fake 
bananas.


More information about the Digitalmars-d-learn mailing list