Repost: make foreach(i, a; range) "just work"

Jesse Phillips Jesse.K.Phillips+D at gmail.com
Tue Feb 25 20:30:44 PST 2014


On Tuesday, 25 February 2014 at 17:34:25 UTC, Regan Heath wrote:
> On Mon, 24 Feb 2014 17:58:51 -0000, Jesse Phillips 
> <Jesse.K.Phillips+D at gmail.com> wrote:
>
>> On Monday, 24 February 2014 at 10:29:46 UTC, Regan Heath wrote:
>>> No, not good enough.  This should just work, there is no good 
>>> reason for it not to.
>>>
>>> R
>>
>> I have long since given up believing this should be in the 
>> language, I'm satisfied with the reasons I gave for why it is 
>> not in the language and why it is not needed to be in the 
>> language.
>>
>> You asked for feedback, I've given mine to you. I'm ok with 
>> you disagreeing with that.
>
> Sure, no worries.  :)
>
> I'd just like to list your objections here and respond to them 
> all, in one place, without the distracting issues surrounding 
> the 3 extra schemes I mentioned.  Can you please correct me if 
> I miss represent you in any way.
>
> 1. Adding 'i' on ranges is not necessarily an index and people 
> will expect an index.
> 2. You don't need to count iterations very often.
> 3. Your point about the "range gets a new value and foreach 
> would compile but be wrong"
> 4. This area of D is not important enough to polish
> 5. We will have enumerate soon, and won't need it.

Seems right, though 4 is, to include this behavior would be a 
breaking change, and the polish on this isn't worth it.

Many of your examples use AA and provide examples where their 
iteration has a "count" variable. I have never wanted to do this, 
and frankly would be extremely concerned for what the code would 
be doing with it. AA are unordered, ranges tend to be ordered, 
but this isn't a requirement.

> I believe objection #3 is invalid.  The foreach in the example 
> given is a flattened tuple foreach, not the range foreach I 
> want to change.  Making the change I want will have no effect 
> on the given example.

I think the examples you are referring to are specific to the 
foreach you desire, but use enumerate to show the current 
behavior. It was not to claim that your addition would break 
tuple flattening.

> I think the strongest objection here is #1, #2 and #4 are 
> fairly subjective and #5 just seems a little odd to me, why 
> would you "want" to type more rather than less?

Yes, #5 comes down to "OMG you'll be able to type lots more text! 
And that is awesome. Who needs this lack of typing."

> For my point of view, it seems an obvious lack in D that the 
> range foreach doesn't have the same basic functionality as the 
> array foreach.  That's pretty much my whole argument, it should 
> just work in the same way as arrays.

Ranges aren't always sequential. It doesn't make sense on many 
types of ranges (random, associative array if it provided a 
range). Arrays on the other hand are sequential by definition.


More information about the Digitalmars-d mailing list