Bring back foreach int indexes

Paolo Invernizzi paolo.invernizzi at gmail.com
Thu Nov 30 08:00:34 UTC 2023


On Wednesday, 29 November 2023 at 22:30:30 UTC, Steven 
Schveighoffer wrote:
> On Wednesday, 29 November 2023 at 21:47:09 UTC, bachmeier wrote:
>>
>> 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.
>
> Understand that I don't disagree with you. But my point is that 
> `foreach(int i; 0 .. arr.length)` *never compiled*, whereas 
> `foreach(int i, v; arr)` did compile (without deprecation) at 
> some point.
>
> This puts it in the category of "features that were removed", 
> vs "features that should be added", and therefore gives a 
> little less resistance to (re)allowing it.
>
> -Steve

I'm totally +1 on this: it's a VERY common case, and it's an 
*explicit* request done to the compiler.

Adding an assert instead of the deprecation it's fine, usually 
it's what we do right after the foreach statement to safely 
transform the ulong to an int.




More information about the Digitalmars-d mailing list