Thread overview
[Issue 9009] allow foreach without identifier
Jun 14, 2014
Nick Treleaven
Dec 17, 2022
Iain Buclaw
June 12, 2014
https://issues.dlang.org/show_bug.cgi?id=9009

hsteoh@quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh@quickfur.ath.cx

--
June 12, 2014
https://issues.dlang.org/show_bug.cgi?id=9009

--- Comment #7 from bearophile_hugs@eml.cc ---
With a little breaking change "_" could become a name that can't be referenced, so you can do:

foreach (_; 0 .. 10) { // OK
  foreach (_; 0 .. 20) { // OK
    writeln(_); // syntax error
  }
}


Such _ can also be used as "don't care" for tuple unpacking:

t{_, b} = fooAB();


And in switch on structs (that contain the optional "unapply" method):

switch (foo) {
  case Foo(x, _, _): writeln(x); break;
  default:
}

--
June 14, 2014
https://issues.dlang.org/show_bug.cgi?id=9009

--- Comment #8 from Nick Treleaven <ntrel-public@yahoo.co.uk> ---
Just to note that a related issue is:

foreach (i, Unused; ...)

This may be more common than the foreach (Unused; ...) case - see:

http://forum.dlang.org/post/lnhelg$g01$1@digitalmars.com

A possible solution for both is to use '__' for the unused variable, but the language enforces that __ is never used and is allowed to shadow:

http://forum.dlang.org/post/vawgobhmtjjjetsegkqj@forum.dlang.org

Note that __ is already a reserved identifier.

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

Iain Buclaw <ibuclaw@gdcproject.org> changed:

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

--