Thread overview
[Issue 9009] New: allow foreach without identifier
Jan 12, 2013
Andrej Mitrovic
Mar 07, 2013
Andrej Mitrovic
Aug 25, 2013
Piotr Szturmaj
Aug 26, 2013
Lionello Lunesu
November 12, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9009

           Summary: allow foreach without identifier
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: monarchdodra@gmail.com


--- Comment #0 from monarchdodra@gmail.com 2012-11-12 07:57:54 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 12, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9009


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

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


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-11 17:50:06 PST ---
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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 13, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9009



--- Comment #2 from monarchdodra@gmail.com 2013-01-13 13:56:29 PST ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 07, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9009


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #3 from bearophile_hugs@eml.cc 2013-03-06 16:15:47 PST ---
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) {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 07, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9009



--- Comment #4 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-03-07 07:08:24 PST ---
(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) { }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 25, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9009


Piotr Szturmaj <pszturmaj@tlen.pl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pszturmaj@tlen.pl


--- Comment #5 from Piotr Szturmaj <pszturmaj@tlen.pl> 2013-08-25 12:50:39 PDT ---
I'd vote for version without a semicolon:

1. foreach (0 .. n)
2. foreach (range)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 26, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9009


Lionello Lunesu <lio+bugzilla@lunesu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lio+bugzilla@lunesu.com


--- Comment #6 from Lionello Lunesu <lio+bugzilla@lunesu.com> 2013-08-25 19:10:53 PDT ---
(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

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