Proposal: takeFront and takeBack
monarch_dodra
monarch_dodra at gmail.com
Tue Jul 3 10:22:17 PDT 2012
On Tuesday, 3 July 2012 at 16:37:20 UTC, Jonathan M Davis wrote:
> ...
> Oh, and if we go with this, ideally, the compiler would be
> updated to use
> takeFront for foreach instead of front and popFront if a range
> implements it
> (but still do it the current way if it doesn't). So, if
> typeof(range)
> implements takeFront,
>
> foreach(e; range) {...}
>
> would then become
>
> for(auto _range = range; !_range.empty;)
> {
> auto e = _range.takeFront();
> ...
> }
>
> instead of
>
> for(auto _range = range; !_range.empty; _range.popFront())
> {
> auto e = _range.front();
> ...
> }
>
> but that's an optimization which could be added later.
>
> - Jonathan M Davis
Great job! I think takeFront, is a really good idea. It would
also finally fix the whole "should pop return a value" debate.
I'd only be afraid the name might clash (in spirit) with the
existing "take", "takeOne" and "takeNone". Not so long ago, I
asked for a way to get the last elements of a subrange, and
proposed a method called "takeBack"...
Maybe "popMoveFront" would be more addequate/Accurate? This would
make it more in line with the existing "moveFront" defined inside
range, As well as the "popFront" which would be inside the range
implementation: It would make the trio "front, popFront,
popMoveFront".
I think wiring it into foreach is also a good idea, but would
require compiler support of course. Personally though, regarding
foreach, I'd appreciate it more if we could first get foreach
with ref to work with "front assign" (
http://forum.dlang.org/thread/ceftaiklanejfhodbpix@forum.dlang.org
) That said, your proposal is probably less costly to implement.
More information about the Digitalmars-d
mailing list