foreach for ranges?

Jonathan M Davis jmdavisProg at gmx.com
Tue Jul 17 12:45:32 PDT 2012


On Tuesday, July 17, 2012 21:38:51 Eyyub wrote:
> On Tuesday, 17 July 2012 at 19:27:54 UTC, Jonathan M Davis wrote:
> > It translates
> > 
> > foreach(e; range)
> > {}
> > 
> > into something like
> > 
> > foreach(auto __range = range; !__range.empty;
> > __range.popFront())
> > {
> > 
> >     auto e = __range.front;
> > 
> > }
> > 
> > The compiler knows just enough about ranges to enable foreach,
> > but beyond
> > that, ranges are entirely a library construct.
> 
> The spec' says "If the foreach range properties do not exist, the
> opApply method will be used instead.", does this mean that range
> properties take precedence over opApply ?

This post gives the current precedence, but there was some discussion of 
adjusting it a bit:

http://forum.dlang.org/post/mailman.275.1342019430.31962.digitalmars-
d at puremagic.com

>From the looks of it, opApply gets precedence right now. But mixing ranges and 
opApply isn't a good idea in most cases, so it doesn't come up very often.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list