foreach

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 12 10:24:59 PDT 2014


On Thu, Jun 12, 2014 at 01:11:12PM -0400, Steven Schveighoffer via Digitalmars-d wrote:
> On Thu, 12 Jun 2014 11:00:11 -0400, Manu via Digitalmars-d
> <digitalmars-d at puremagic.com> wrote:
> 
> >I often find myself wanting to write this:
> >  foreach(; 0..n) {}
> >In the case that I just want to do something n times and I don't
> >actually care about the loop counter, but this doesn't compile.
> 
> The compiler has to assign a variable to this somehow so it can
> increment and test. Naming it isn't the worst thing in the world. As
> others have said, pick an uncommon name.

A variable doesn't require a name, if user code never references it. All
the compiler cares about is that a symbol is created for it, that can be
used when generating the loop condition code. Anonymous functions don't
have names either, and the compiler handles that just fine.  (Well, OK,
an internal unique identifier is generated for it, but that's
essentially what's being asked for here by the OP.) Or, for that matter,
ignored function arguments:

	void delegate(int) dg = (int) {
		// Name not needed for ignored argument here.
	};


--T


More information about the Digitalmars-d mailing list