Bring back foreach int indexes

Steven Schveighoffer schveiguy at gmail.com
Wed Nov 29 16:25:55 UTC 2023


On Wednesday, 29 November 2023 at 16:06:32 UTC, bachmeier wrote:
>
> Along the same lines, this won't even compile:
>
> ```
> foreach(int idx; 0..arr.length) {
> }
> ```
>
> Apparently someone decided the explicit `int` is an implicit 
> conversion.

I don't think that ever compiled, even in D1 (64-bit). What 
happens here is the two ends are converted to a common type (in 
this case size_t), and you can't assign size_t to int.

But it's easy to fix, just change the top condition via a cast or 
conversion. The same is not available to a foreach over an array 
with an index.

I would be fine fixing this to work in the same way as I 
suggested (with a pre-loop assert that it will work). It makes 
sense that it should act the same.

-Steve


More information about the Digitalmars-d mailing list