foreach range with index

Luís Marques via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 13 19:42:46 PDT 2017


On Tuesday, 13 June 2017 at 21:44:43 UTC, Steven Schveighoffer 
wrote:
> But I think leaving the definition of the index up to the range 
> itself is paramount -- I don't want every range to be able to 
> have a size_t index, as that's not always what you want, and it 
> conflicts with other items. What we may need is a smarter way 
> to get from the type parameters at the front of the foreach to 
> an iterable type.

That's why I mentioned random access ranges, as in that case we 
can sidestep this discussion; since foreach understands input 
ranges, why can't foreach understand random access ones, and 
iterate them as if they were slices, including maintaining a 
foreach-generated index? That is, it would have nothing to do 
with tuple unpacking. Plus, it would work transparently in the 
cases you replace `slice` with `slice.algorithm`, where algorithm 
maintains random-access. Instead of having to add .enumerate in 
each place the slice (now a range) is iterated, it would just 
work.


More information about the Digitalmars-d mailing list