Jump to page: 1 2
Thread overview
Deprecation: foreach: loop index implicitly converted from size_t to int
Jan 18, 2019
Michael
Jan 18, 2019
Nicholas Wilson
Jan 18, 2019
Adam D. Ruppe
Jan 18, 2019
Michael
Jan 18, 2019
Jonathan M Davis
May 03
BoQsc
May 03
user1234
May 03
BoQsc
May 03
user1234
May 03
user1234
6 days ago
BoQsc
January 18, 2019
Hello all,

I am getting this deprecation warning when compiling using DMD64 D Compiler v2.084.0 on Linux. I'm a little unsure what the problem is, however, because the code producing these warnings tends to be of the form:

> foreach (int i, ref prop; props)

This, to be, looks like quite the explicit conversion, no? Does it mean I now have to use to!int(i) to convert the type of i in a foreach now?

Thanks,
Michael.
January 18, 2019
On Friday, 18 January 2019 at 12:27:17 UTC, Michael wrote:
> Hello all,
>
> I am getting this deprecation warning when compiling using DMD64 D Compiler v2.084.0 on Linux. I'm a little unsure what the problem is, however, because the code producing these warnings tends to be of the form:
>
>> foreach (int i, ref prop; props)
>
> This, to be, looks like quite the explicit conversion, no? Does it mean I now have to use to!int(i) to convert the type of i in a foreach now?
>
> Thanks,
> Michael.

> foreach (int i, ref prop; props)

All you need to do is

> foreach (i, ref prop; props)

The reason for the deprecation is that if your array props is > 2GB int can't span the range of indices necessary because it will overflow.
January 18, 2019
On 1/18/19 7:27 AM, Michael wrote:
> Hello all,
> 
> I am getting this deprecation warning when compiling using DMD64 D Compiler v2.084.0 on Linux. I'm a little unsure what the problem is, however, because the code producing these warnings tends to be of the form:
> 
>> foreach (int i, ref prop; props)
> 
> This, to be, looks like quite the explicit conversion, no? Does it mean I now have to use to!int(i) to convert the type of i in a foreach now?

That's one possibility.

You can avoid to!int by using a mask or a cast:

foreach(_i, ref prop; props)
{
   int i = _i & 0xffff_ffff;
   auto i2 = cast(int)_i;
}

It's less than ideal, but the reason is that there is a possibility that props could have 2^31 or more elements, in which case int will not cut it. It's forcing you to make that decision that it's OK vs. the compiler making that assumption. Any time the compiler is throwing away data without a cast, D tends to require buy in from the developer.

Note that this is much more of a problem with smaller types (short or byte), but it would be inconsistent not to also flag int as problematic.

I would recommend just using foreach(i, ref prop; props) and casting only where it's absolutely necessary.

-Steve
January 18, 2019
On Friday, 18 January 2019 at 12:27:17 UTC, Michael wrote:
> This, to be, looks like quite the explicit conversion, no?

Yeah, I agree. But the language is silly. I just leave the type out of foreach and explicitly cast it inside the body.
January 18, 2019
On Friday, 18 January 2019 at 13:29:29 UTC, Adam D. Ruppe wrote:
> On Friday, 18 January 2019 at 12:27:17 UTC, Michael wrote:
>> This, to be, looks like quite the explicit conversion, no?
>
> Yeah, I agree. But the language is silly. I just leave the type out of foreach and explicitly cast it inside the body.

Thank you all for the concise explanations and suggestions, I think that's fairly straightforward. I thought perhaps I was doing the sensible thing of dealing with the conversion inside the foreach statement, but I guess not!
January 18, 2019
On Friday, January 18, 2019 8:34:22 AM MST Michael via Digitalmars-d-learn wrote:
> On Friday, 18 January 2019 at 13:29:29 UTC, Adam D. Ruppe wrote:
> > On Friday, 18 January 2019 at 12:27:17 UTC, Michael wrote:
> >> This, to be, looks like quite the explicit conversion, no?
> >
> > Yeah, I agree. But the language is silly. I just leave the type out of foreach and explicitly cast it inside the body.
>
> Thank you all for the concise explanations and suggestions, I think that's fairly straightforward. I thought perhaps I was doing the sensible thing of dealing with the conversion inside the foreach statement, but I guess not!

Well, you were really doing the equivalent of simply declaring a variable without a cast. e.g.

int i = arr.length;

rather than

int i = cast(int)arr.length;

In general, if the compiler treated giving the foreach variable an explicit type as being a cast, it would make it really easy to screw up and unknowingly give a different type than the actual type of the values and end up with an invisible cast, which could cause subtle bugs.

IIRC, the only case where foreach treats giving an explict type as anything like a cast is when you're iterating over a string type, and you give a character type different from the character type of the string. In that case, it actually decodes the string from one Unicode encoding and encodes it in the other. Whether the language should have done that rather than requiring that a library solution be used is debatable (I believe that it far predates Phobos having the Unicode handling that it does now), but at least it can't result in stuff like silent truncation. Worst case, it has a silent performance hit, or you get an unexpected UnicodeException at runtime due to invalid Unicode.

- Jonathan M Davis



May 03

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.

May 03

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.

May 03

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

May 03

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 ?

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.

« First   ‹ Prev
1 2