foreach range with index
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Wed Jun 14 12:22:24 PDT 2017
On 6/14/17 12:35 PM, Luís Marques wrote:
> On Wednesday, 14 June 2017 at 16:17:50 UTC, Steven Schveighoffer wrote:
>> I don't know if there is room to allow range-defined indexes and
>> foreach-defined indexes. foreach(a, b; someRange) has to do one or the
>> other. Otherwise, changes to how someRange operates can make this a
>> completely different operation.
>
> Seems like exactly what we want. If you add a customization to someRange
> to change the iteration behavior then each relevant foreach
> automatically switches from the default to the requested behavior.
> That's exactly the point of programming to abstract interfaces.
For example:
foreach(i, v; hashmap) => i is counter, v is value
Later hashmap adds support for iterating key and value. Now i is key, v
is value. Code means something completely different.
Compare with
foreach(i, v; hashmap.enumerate)
Intent is clear from the code.
-Steve
More information about the Digitalmars-d
mailing list