Thread overview
[Issue 12488] foreach() should accept (and ignore) 'auto' in ForeachType
Apr 21, 2014
Andrej Mitrovic
Aug 24, 2014
Ketmar Dark
Aug 24, 2014
Ketmar Dark
Aug 24, 2014
Ketmar Dark
Dec 17, 2022
Iain Buclaw
April 21, 2014
https://issues.dlang.org/show_bug.cgi?id=12488

Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com

--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> ---
I don't think this makes any sense. If we were forced to either write auto or ref, then yeah this would actually save us from some bugs, e.g. iterating over a struct array by value (a very common bug IMHO):

-----
struct S { int x; }

void main()
{
    S[] arr;
    arr.length = 2;

    foreach (s; arr)
        s.x = 10;

    // fails
    assert(arr == [S(10), S(10)]);
}
-----

In this case if we were force to use 'auto' or 'ref' then this bug would be easier to catch.

But adding support for 'auto' without requiring a change will do us no good. People will start questioning "why is there an auto if it doesn't do anything", and it's always a bad idea to have two syntaxes that do the exact same thing. It will only cause confusion.

--
August 24, 2014
https://issues.dlang.org/show_bug.cgi?id=12488

Ketmar Dark <ketmar@ketmar.no-ip.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Attachment #1341|0                           |1
        is obsolete|                            |

--- Comment #3 from Ketmar Dark <ketmar@ketmar.no-ip.org> ---
Created attachment 1387
  --> https://issues.dlang.org/attachment.cgi?id=1387&action=edit
new foreach-auto patch

2Andrej Mitrovic: i completely agree that 'auto' must be forced, but we can't do this without breaking the code. hence i made 'auto' optional.

and i have a brand new patch, which allows even more useless things! now it allows things like:

  foreach (; ...)
  foreach (auto; ...)
  foreach (n, auto; ...)

--
August 24, 2014
https://issues.dlang.org/show_bug.cgi?id=12488

Ketmar Dark <ketmar@ketmar.no-ip.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Attachment #1387|0                           |1
        is obsolete|                            |

--- Comment #4 from Ketmar Dark <ketmar@ketmar.no-ip.org> ---
Created attachment 1388
  --> https://issues.dlang.org/attachment.cgi?id=1388&action=edit
updated patch

patch updated. now things like "foreach (int n; [0, 1, 2, 3]) {}" are allowed.

--
August 24, 2014
https://issues.dlang.org/show_bug.cgi?id=12488

--- Comment #5 from Ketmar Dark <ketmar@ketmar.no-ip.org> ---
i meant "foreach (int; [0, 1, 2, 3]) {}"

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=12488

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P4

--