foreach for ranges?

Jonathan M Davis jmdavisProg at gmx.com
Tue Jul 17 12:27:34 PDT 2012


On Tuesday, July 17, 2012 20:59:12 Mike L. wrote:
> How exactly does the compiler know how to do foreach on ranges
> (for example, ones returned by std.algorithm.map ?) I've looked
> around in std.algorithm and std.range, but can't seem to figure
> it out.

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.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list