Bring back foreach int indexes
bachmeier
no at spam.net
Wed Nov 29 21:47:09 UTC 2023
On Wednesday, 29 November 2023 at 16:25:55 UTC, Steven
Schveighoffer wrote:
> On Wednesday, 29 November 2023 at 16:06:32 UTC, bachmeier wrote:
>> 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.
Yeah, but it's a matter of ugliness. This looks awful
```
foreach(idx; 0..(cast(int) arr.length)) {
}
```
In the case you're talking about, you could do
```
foreach(_idx, v; arr) {
int idx = cast(int) _idx;
}
```
I don't mind ugly and verbose code if there's sufficient benefit.
There's no benefit in this case.
More information about the Digitalmars-d
mailing list