foreach counter now must be size_t ?
Rubn
where at is.this
Mon Feb 4 23:57:50 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.
I remember someone pointing out that foreach_reverse didn't
implicitly cast to int, but foreach did. Kind of sad they went to
opposite way. Very rarely are you going to have an array that
will fill a uint. If your type is only 1 byte that's 4 GB of
memory. That's not a very common use case. If it's 4 bytes it
grows to 16 GB which is more than the average consumer device
has. Having to deal with length array being different for 32 and
64-bit is a pain. I ended up creating a function "length32" to
avoid that garbage. If I ever have an array that can fill 32-bit
then something has gone very wrong (in my use case).
More information about the Digitalmars-d
mailing list