Deprecation: foreach: loop index implicitly converted from size_t to int
user1234
user1234 at 12.de
Fri May 3 15:19:13 UTC 2024
On Friday, 3 May 2024 at 14:59:57 UTC, BoQsc wrote:
> On Friday, 3 May 2024 at 13:18:02 UTC, user1234 wrote:
>> On Friday, 3 May 2024 at 10:50:03 UTC, BoQsc wrote:
>>> [...]
>>
>> **You can specify the index type, just choose the right one.**
>> For now there's a deprecation message but after some while
>> you'll get a proper error message, e.g _"index type for arr
>> must be of type T because arr.length type is T"_.
>>
>> What's is happening now is to help people updating their code
>> and prevent abrupt breakages.
>
>
>
>
> 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.
More information about the Digitalmars-d-learn
mailing list