DMD 0.170 release (foreach_reverse)

John Reimer terminal.node at gmail.com
Wed Oct 25 19:38:12 PDT 2006


On Wed, 25 Oct 2006 18:45:25 -0700, Bill Baxter  
<dnewsgroup at billbaxter.com> wrote:

> Walter Bright wrote:
>
>> The saving grace about foreach_reverse is:
>>  1) Only a small (5% ?) of loops will be reverse, so you won't see it  
>> or need to type it that often.
>
> A good reason why it doesn't deserve its own keyword.
>

Agree.


>> 2) It's very clear what it does.
>
> And  'foreach(i; list.reversed)' is pretty clear as well.
>


Agree again... That looks much bettter!  For arrays, I'm sure such a  
property could be special-cased to remove any risk of creating a new  
reversed array in place.


>> 3) It isn't likely to conflict with other names.
>
> Because it's a 15 letter keyword with an underscore!  While we're at it,  
> let's make a for_i_equals_one_to_ten keyword, too.  I loop from 1 to 10  
> a lot in my code (well, not that much really <5%, but at least it's a  
> name that's not likely to conflict with other names).
>


Agree.


>> 4) It is analogous with C++'s reverse_iterator names.
>
> But those aren't keywords.  And even more analogous is STL's  
> std::for_each in the standard header 'algorithm'.
>     http://www.sgi.com/tech/stl/for_each.html
> Note that STL does not have a std::for_each_reverse.  You get that  
> behavior, as you might expect, by using a reverse iterator instead of a  
> forward iterator as the argument.
>
>


Agree... I'm afraid I'm already biased against any train of thought that  
basically reduces to "so D can do it /like/ C++".  But, alas, Walter is  
biased oppositely (darn diodes).


> And in another message Walter Bright wrote:
>
>  > foreach_reverse was pretty trivial to implement. All the machinery  
> was > already there.
>
> That should be setting off warning bells in your head, too.  "The  
> machinery is already there" is just another way of saying "this feature  
> is mostly redundant". The foreach machinery already does the job.  
> foreach_reverse is more or less just a copy-paste of that code with all  
> references to "opApply" replaced with "opApplyReverse".  So of course  
> it's trivial to implement.
>
> I'll repeat a line from the "zen of python" one more time, at the risk  
> of being told that 'D isn't Python', because I think it's true  
> regardless of the language:
>
>    "Special cases aren't special enough to break the rules."
>
> As I understand it, the only real reason for foreach_reverse, when it  
> comes down to it, is because of a strong desire to have a special case  
> fast reverse iteration for arrays and strings.  But it really should be  
> possible to hide that special case from the user and just detect some  
> particular property e.g. "some_array.reversed" in a foreach statement.  
> It may be a little more work (and maybe you just punt on the  
> optimization till 2.0), but in the long run, the language and all it's  
> users will benefit from removing that special case.
>


I've heard Walter's arguments, and I acknowledge some of his reasons  
aren't completely arbitrary (are they ever?).  Nonetheless, I still think  
foreach_reverse is a mistake and a cheap addition to D.  I really hope it  
doesn't last in its current form.  Other people may find it easy to accept  
foreach_reverse because (1) it fixes a problem they see in front of them  
here and now (in which case it's very hard to refuse the quick fix) or (2)  
they are getting tired of arguing with Walter every time a ugly feature  
gets added, and they figure it's not worth the effort to argue the matter  
out. :)  Either way, I think most of us are giving up.  Unfortunately,  
this is the way the D design continues to be done.

At the same time, I do feel sorry for Walter when I see all the language  
feature suggestions that go on in this newsgroup.  A whole lot of people  
seem to have there pet feature that they want in D.  It's scary to think  
of what would happen if everyone got their wish. :P

All I hope, in this situation, is that Walter starts agreeing with Bill's  
points.

-JJR



More information about the Digitalmars-d-announce mailing list