random access-range without lower-power range kinds?

Jesse Phillips jessekphillips+D at gmail.com
Tue Dec 14 09:29:20 PST 2010


Lars T. Kyllingstad Wrote:

> To avoid the boilerplate, you could write a mixin that defines the 
> iteration primitives for you.
> 
>   mixin template IterationFuncs()
>   {
>       int index;
>       bool empty() { return index == length; }
>       auto front() { return opIndex(index); }
>       void popFront() { ++index; }
>       // ... etc.
>   }
> 
> Then you'd just have to define opIndex() and length(), and the mixin does 
> the rest for you.
> 
>   struct MyRange(T)
>   {
>       T opIndex(int i) { ... }
>       @property int length() { ... }
>       mixin IterationFuncs!();
>   }
> 
> (I haven't tested the code above, so it probably has bugs, but you get 
> the point.)
> 
> -Lars

Maybe this should be added to std.range?

http://d.puremagic.com/issues/show_bug.cgi?id=5351


More information about the Digitalmars-d-learn mailing list