Enhancing foreach

Jonathan M Davis jmdavisProg at gmx.com
Wed Jan 9 18:27:24 PST 2013


On Thursday, January 10, 2013 03:04:56 ixid wrote:
> Regardless of this particular suggestion's value, I think you're
> wrong to dismiss readable terseness and saving typing as
> mattering, it's one of D's advantages over C++ that it makes a
> lot of things far easier to do and understand because they're not
> a horrid mess.

Terseness can be nice, but not at the cost of clarity (and there's a major 
loss of clarity with your suggestion). There's also a definite cost to 
providing multiple ways to do exactly the same thing. It makes the language 
more complicated and increases its cognitive load. It's that much worse if the 
construct in question is relatively rare. One case where tersness has been 
quite valuable is being able to omit parens for single-argument templates, but 
unlike your foreach suggestion, it's relatively obvious what's going on, and 
it's used quite frequently (sometimes multiple times in a single statement). 
Iterating over a list of numbers like with

foreach(i; 0 .. 5)
{}

is much less frequent in comparison. There is a cost to any feature to the 
language, and so features must pull their own weight. _All_ that this 
suggestion does is save a few characters in a construct that in my experience 
is occasionally useful but not heavily used. And it comes with a cost to 
clarity.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list