foreach

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 12 20:44:09 PDT 2014


On Fri, 13 Jun 2014 12:26:34 +1000
Manu via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> It gets awkward when you nest, using '_' leads to '__', and
> personally, I would expect an 'unreferenced variable' warning for the
> unused loop counter. I like warnings hassling me about unused
> variables.

I don't expect that to ever happen in D. There are too many places where
variables are unused on purpose - especially in code used for template
constraints (e.g. isInputRange has an unused variable in it). Having warnings
about unused variables could then not only be counter-productive, but when
combined with -w, they could easily break a lot of code.

> I also object to the inconsistency with for(;;). Recall Scott Myers
> talk...

Personally, I don't think that that should even be legal. while(1) works just
as well without requiring any special cases.

> It's theoretically an optimisation too; capturing front may be a
> costly operation that's not required. Ranges maintain their counters
> internally, there's no reason to emit code to capture a local copy of
> 'front' if it's not used. popFront and empty don't imply a byVal copy,
> they usually just update range counters.

That's an interesting idea. However, I would expect it to be a rare benefit,
particularly when the primary use case is with integers.

- Jonathan M Davis


More information about the Digitalmars-d mailing list