access foreach index counter for Iterate n times

mw mingwu at gmail.com
Sat May 21 03:01:39 UTC 2022


On Saturday, 21 May 2022 at 02:40:06 UTC, Ali Çehreli wrote:
> On 5/20/22 19:23, mw wrote:
>
> > But isn't iota as a function call also expensive?
>
> I am sure someone will show some disassembly to compare. :)
>

from Max:

> https://d.godbolt.org/z/TsKbe96dv

Nice assembly.



>
> The biggest issue there is the number range, which is a weird 
> thing in the language.
>
> > My point: the language should be more consistent.
>
> The automatic loop counter is available only for arrays. If we 
> see number ranges things other than arrays (they are not 
> arrays), then there is no consistency issue.

Yes! let's call it "loop counter", regardless the user is looping 
over array, or (number) range ...: so, "loop counter" as a 
language concept.


> One could argue to remove number ranges from the language but I 
> don't think it's that big of a deal.


Why not keep it, and the compiler auto translate

foreach (i, e; 5 .. 10)

to:

foreach(i, e; iota(5,11).enumerate)


esp. after seeing the assembly.


More information about the Digitalmars-d mailing list