foreach bug, or shoddy docs, or something, or both.
Dave Jones
dave at jones.com
Sun Dec 10 11:40:08 UTC 2017
On Sunday, 10 December 2017 at 02:31:47 UTC, Jonathan M Davis
wrote:
> On Sunday, December 10, 2017 02:02:31 Dave Jones via
> Digitalmars-d wrote:
> https://issues.dlang.org/show_bug.cgi?id=14984
>
> Honestly, it would have never occurred to me to try and modify
> the variables declared in the foreach like that, and my first
> inclination is to think that it shouldn't be allowed, but
> thinking it through, and looking at how things actually work, I
> don't think that the current behavior is really a problem.
Its not something I normally do but I was porting some C++ code
and changed the fors to foreaches. Just it took a while to figure
out what was going wrong.
> then you can increment i the way you want to. So, maybe
> modifying the loop variable in something like
>
> foreach(i; 0 .. 10)
> {
> ++i;
> }
>
> should be disallowed, but I don't really think that the current
> behavior is really a problem either so long as it's properly
> documented. Modifying i doesn't really cause any problems and
> ref or the lack thereof allows you to control whether it
> affects the loop. Ultimaetly, it looks to me like what we
> currently have is reasonably well designed. It doesn't surprise
> me in the least if it's not well-documented though.
Well it's just one of those "APIs should be hard to use in the
wrong way" kinda things to me. Either the loop var should be
actually the loop var rather than a copy, or it should be an
error to modify it.
Anyway it's not a bit deal like you say, but should be better
explained in the docs at least.
Thanks,
More information about the Digitalmars-d
mailing list