Deprecation: foreach: loop index implicitly converted from size_t to int
BoQsc
vaidas.boqsc at gmail.com
Fri May 3 14:59:57 UTC 2024
On Friday, 3 May 2024 at 13:18:02 UTC, user1234 wrote:
> On Friday, 3 May 2024 at 10:50:03 UTC, BoQsc wrote:
>> Why am I forced to visit this D Lang thread, why this
>> deprecation warning still appears in my console window in the
>> latest version of DMD. Does not make any sense from the
>> developer's perspective to show this warning and pollute the
>> already polluted logging entries of the compiler. How am I
>> suppose to program anything effectively if half of the screen
>> are some nonsensical deprecation warnings without guidance or
>> sane explanations.
>>
>> This is not better
>> ```
>> foreach (i, row; arr)
>> ```
>> than
>> ```
>> foreach (int i, row; arr)
>> ```
>> Hides the datatype and makes the D language appear in-explicit
>> and annoying.
>>
>> What is this language becoming. A completely weak typed
>> language or something?
>>
>> I would use JavaScript if I would want that. How are we
>> suppose to make whole sane Operating Systems with such
>> syntaxes. Do everyone just enjoy having bugs with some
>> implicit size_t, or do everyone just enjoy deprecation
>> warnings in their logging systems when there are way more
>> important problems to solve, that are actually project related.
>
> **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
More information about the Digitalmars-d-learn
mailing list