foreach counter now must be size_t ?

Rubn where at is.this
Sat Feb 9 00:15:41 UTC 2019


On Friday, 8 February 2019 at 08:06:39 UTC, Nicholas Wilson wrote:
> On Thursday, 7 February 2019 at 23:02:14 UTC, Rubn wrote:
>> I'll take that to mean you don't think this is a bug:
>>
>> @safe void foo(int[] a) {
>>     for(int i = 0; i < a.length; ++i ) {
>>        writeln( i );
>>     }
>> }
>>
>> Fucking classic.
>
> Its not a bug. In the case that a.length > int.max, the loop 
> will not terminate and only print indices. If the loop was
>
>      for(int i = 0; i < a.length; ++i ) {
>         writeln( a[i] );
>      }
>
> it would still be @safe, but the program would crash because 
> the index would (eventually) be OOB. If a.length > uint.max and 
> the loop was
>
>      for(uint i = 0; i < a.length; ++i ) {
>         writeln( a[i] );
>      }
>
> then the loop fail to terminate, and it would still be @safe. 
> All the above problems are avoided using size_t as the index.

TIL infinite loop that freezes program isn't a bug.


More information about the Digitalmars-d mailing list