[Issue 6652] foreach parameter with number range is always ref

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jun 15 19:55:34 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=6652


Ryuichi OHORI <r.97all at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |r.97all at gmail.com


--- Comment #11 from Ryuichi OHORI <r.97all at gmail.com> 2012-06-15 19:57:43 PDT ---
(In reply to comment #9)
> I prefer the number 2. I think it breaks none of my programs.
> 
> The number 3 is a trap, because it silently changes the semantics of old D
> code. And it's bug-prone for new D programmers too because they can change the
> variable by mistake. Generally immutable variables are safer.

In my point of view, as a newcomer to D, more bug-prone is the current
behavior.

Foreach statement provides iteration over arrays, ranges, etc, and notation of
range "0..n" also *looks like* a collection. So, foreach range statements
should work like foreach over collection.

I have wrote to stuck in my program:
foreach (i; 0..M^^n)
{
    foreach (j; 0..n)
    {
        a[j] = i % M;
        i /= M;
    }
    // operations which use a but i
}
which I wrote in Python before:
for i in range(M**n):
    for j in range(n):
        a[j] = i % M
        i /= M
    # operations which use a but i
and was sad to see an infinite loop.

Even a new programmer *intends* to change the value of i when changing, if not
just a typo.
If someone want to affect loop, s/he can write
i = 0;
while (i < 10)
{
    // operations which change the value of i
    i += 1;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list