foreach
Manu via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jun 12 20:34:27 PDT 2014
On 13 June 2014 13:04, Daniel Murphy via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> "Manu via Digitalmars-d" wrote in message
> news:mailman.2111.1402626404.2907.digitalmars-d at puremagic.com...
>
>
>> It gets awkward when you nest, using '_' leads to '__',
>
>
> i,j,k,etc work just fine. Are you really nesting your loops that deeply?
Giving explicit names pollutes the local namespace, and the point
below about unreferenced variable warnings if you give explicit names.
>> and
>> personally, I would expect an 'unreferenced variable' warning for the
>> unused loop counter. I like warnings hassling me about unused
>> variables.
>
>
> This is a good point.
>
>
>> I also object to the inconsistency with for(;;). Recall Scott Myers
>> talk...
>
>
> Should we also allow "foreach(;)" ? 'for' being so loose is not necessarily
> something we want to copy.
Well, I don't think it really makes sense in the case of forach,
for(;;) loops indefinitely in lieu of a termination condition,
foreach(;) would theoretically be a noop. For each thing in nothing
would iterate no things zero times; it should probably be an
unreachable code warning, given the loop body wouldn't execute.
>> 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.
>
>
> The compiler's optimizer will do that just fine.
Not necessarily. The range might be a lib, and non-pure. The compiler
can't optimise/inline/eliminate the call if it doesn't have code.
In my experience this is common, I frequently wrap C api's in D-style
ranges. Potentially a lot of unnecessary calls to
C_API_GetItem(itemIndex).
More information about the Digitalmars-d
mailing list