Bug 9009 – allow foreach without identifier

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-11-12T07:57:54Z
Last change time
2024-12-13T18:02:47Z
Assigned to
No Owner
Creator
monarchdodra
Moved to GitHub: dmd#17564 →

Comments

Comment #0 by monarchdodra — 2012-11-12T07:57:54Z
Minor enhancement request, but the language should allow using "foreach" without specifying an iteration name. THis can be useful when you want to iterate a fixed amount, without caring about the index. For example, when you want to "popFrontN exactly" (popFrontN is safe, so slightly slower), you'd want to write: foreach( ; 0 .. n ) r.popFront(); Right now, if you do this, you get: main.d(5): Error: basic type expected, not ; main.d(5): Error: no identifier for declarator int for can do without declarators, I don't see why foreach can't have the above syntax. Just a minor ER, but I think it would make foreach that little extra user friendly.
Comment #1 by andrej.mitrovich — 2013-01-11T17:50:06Z
I think this is a worthy request, although you can use "_" as an unused iteration variable you may end up having to invent unique names with 2 or more nested foreach loops: foreach(_; 0 .. n ) { foreach(__; 0 .. m) { // statements } } Since foreach lowers to a for statement I think it should be possible to implement the request. Can we get a pre-approval from Walter/Andrei?
Comment #2 by monarchdodra — 2013-01-13T13:56:29Z
(In reply to comment #1) > I think this is a worthy request, although you can use "_" as an unused > iteration variable you may end up having to invent unique names with 2 or more > nested foreach loops: > > foreach(_; 0 .. n ) { > foreach(__; 0 .. m) { > // statements > } > } > > Since foreach lowers to a for statement I think it should be possible to > implement the request. > > Can we get a pre-approval from Walter/Andrei? BTW, given the compiler error that is spit out by DMD upon writing //---- foreach ( ; 0 .. n ) //---- Error: basic type expected, not ; Error: no identifier for declarator int //---- I'd say it is either a bug that this isn't already supported anyway, or the compiler error should be re-written.
Comment #3 by bearophile_hugs — 2013-03-06T16:15:47Z
I think this is a simple nice idea. There is no point in requiring to name a variable that will not used. It's like in function signatures: void foo(int, int y) {}
Comment #4 by andrej.mitrovich — 2013-03-07T07:08:24Z
(In reply to comment #3) > I think this is a simple nice idea. There is no point in requiring to name a > variable that will not used. It's like in function signatures: > > void foo(int, int y) { } He meant: void foo(int, int) { }
Comment #5 by pszturmaj — 2013-08-25T12:50:39Z
I'd vote for version without a semicolon: 1. foreach (0 .. n) 2. foreach (range)
Comment #6 by lio+bugzilla — 2013-08-25T19:10:53Z
(In reply to comment #5) > I'd vote for version without a semicolon: > > 1. foreach (0 .. n) > 2. foreach (range) Yes, I'd prefer that too. It's what newbies would write. foreach(; ...) is just too hard to explain to newcomers.t
Comment #7 by bearophile_hugs — 2014-06-12T17:08:58Z
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: }
Comment #8 by nick — 2014-06-14T12:39:02Z
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/[email protected] 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/[email protected] Note that __ is already a reserved identifier.
Comment #9 by robert.schadek — 2024-12-13T18:02:47Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17564 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB