foreach range with index

Luís Marques via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 14 15:46:35 PDT 2017


On Wednesday, 14 June 2017 at 19:22:24 UTC, Steven Schveighoffer 
wrote:
> 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.

If we take a step back, I think we are discussing the 
generalization of foreach. Let's assume we want to preserve the 
current foreach behavior where associative types iterate with key 
and value pairs. If the "hashmap" in your example was supposed to 
be an associative type then `i` wasn't a counter, it was the key 
already. If "hashmap" didn't define the iteration key (using a 
new kind of opApply or whatever) then it wasn't an associative 
type, and therefore your change wouldn't be valid.

So, we just have to be careful that when foreach(i, v; foo) is 
introduced the relevant types in phobos also implement the new 
behavior, so that this situation doesn't arise afterwards..


More information about the Digitalmars-d mailing list