Higher Order Range Pattern

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 22 08:04:10 PDT 2015


Hi,
I recently came across the following code:
http://wiki.dlang.org/Higher_Order_Range_Pattern


I can't understand why the properties and methods of the 
structure are called in the correct order.
Why are the property `back()` and the method `popBack()` are not 
called even once?
In general, please explain how it all works.

```
import std.stdio, std.range;

struct Retro(Range)
{
     @property
     {
         auto ref front() { debug writeln("back"); return 
range_.back;  }
         auto ref back()  { debug writeln("front"); return 
range_.front; }
         bool empty()     { debug writeln("empty"); return 
range_.empty; }
     }

     void popFront() { debug writeln("popBack"); range_.popBack(); 
}
     void popBack()  { debug writeln("popFront"); 
range_.popFront(); }
	
     Range range_;
}

auto retro(Range)(Range range)
{
     return Retro!Range(range);
}

void main()
{
     import std.algorithm;
	
     auto arr = [1, 2, 3, 4, 5];
     assert(equal(retro(arr), [5, 4, 3, 2, 1]));
}
```
http://rextester.com/FMPGS76502


More information about the Digitalmars-d-learn mailing list