foreach counter now must be size_t ?

Rubn where at is.this
Tue Feb 5 00:00:24 UTC 2019


On Monday, 4 February 2019 at 23:01:06 UTC, Nicholas Wilson wrote:
> On Monday, 4 February 2019 at 22:26:08 UTC, Johan Engelen wrote:
>> This code now gives a deprecation message (>= 2.084):
>> ```
>> void foo(int[] arr) {
>>     foreach (uint i, ref elem; arr) { }
>> }
>> ```
>> Deprecation: foreach: loop index implicitly converted from 
>> `size_t` to `uint`
>>
>> This is in contrast to the spec that says that "The index must 
>> be of int, uint, long or ulong type, it cannot be ref, and it 
>> is set to be the index of the array element."
>> https://dlang.org/spec/statement.html#foreach_over_arrays
>>
>> Did someone forget to update the spec? Or is it a frontend bug?
>>
>> -Johan
>
> Thats a spec problem. The behaviour is very much deliberate.

Same goes for "sizeof32" which is especially annoying when 
sometimes it evaluates to int because the value is known at the 
time and other times it evaluates to size_t cause it can't 
determine the value right away or is being used at runtime. If 
you a struct that can fill a uint something has gone extremely 
wrong and your executable file is going to be 4 GB minimum to 
accomodate the .init data.


More information about the Digitalmars-d mailing list