Deprecation: foreach: loop index implicitly converted from size_t to int
user1234
user1234 at 12.de
Fri May 3 15:28:53 UTC 2024
On Friday, 3 May 2024 at 15:19:13 UTC, user1234 wrote:
> On Friday, 3 May 2024 at 14:59:57 UTC, BoQsc wrote:
>> On Friday, 3 May 2024 at 13:18:02 UTC, user1234 wrote:
>>> [...]
>>
>>
>>
>>
>> So how would you update this example, what is the right index
>> type here to choose?
>>
>> ```
>> import std.stdio : writefln;
>>
>> void main() {
>> auto arr = [
>> [5, 15], // 20
>> [2, 3, 2, 3], // 10
>> [3, 6, 2, 9], // 20
>> ];
>>
>> foreach (i, row; arr)
>> {
>> double total = 0.0;
>> foreach (e; row)
>> total += e;
>>
>> auto avg = total / row.length;
>> writefln("AVG [row=%d]: %.2f", i, avg);
>> }
>> }
>> ```
>>
>> Example taken from
>> https://tour.dlang.org/tour/en/basics/foreach
>
> Isn't that obvious ?
>
> ```d
> foreach (const size_t i, row; arr)
> ```
>
> `arr` is not a static array, it is a dynamic one, consequently
> its `.length` type is `size_t`, even if you have the feeling
> that, in the present situation, `int` bitwidth would be
> sufficient.
even better:
```d
foreach (const typeof(arr.length) i, row; arr)
```
Otherwise I respect your POV, it's just that here I have no
problem with the way that works. I dont see any issue with the
type system. D type system is static, strong, but optionally
inferred. And that's it.
More information about the Digitalmars-d-learn
mailing list