Downgrading ranges

Lars T. Kyllingstad public at kyllingen.net
Sun Jun 9 05:19:46 PDT 2013


A recent pull request discussion got me thinking:  The ability to 
"downgrade" a range to a less featureful one -- wrapping a random 
access range in an input range, say -- can be very useful 
sometimes, particularly for testing.  The pull request in 
question was Walter's LZ77 module, where he has added an input 
range type solely for the purpose of ensuring that all code paths 
are exercised in unittests.  Coincidentally, I currently find 
myself in need of the exact same functionality for a piece of 
range code I'm working on.

I think this would be a generally useful thing, enough so to 
warrant its inclusion in std.range.  It's also rather trivial to 
implement.  The question is, what is a good API?  I'm thinking a 
single type which can be instantiated differently based on 
template parameters.  It is important that the range never 
aliases itself away to the wrapped type, as that would somewhat 
defeat its purpose.

Example:

enum RangeFeatures
{
     input,
     forward,
     randomAccess,
     ...
}

struct RestrictedRange(Range, RangeFeatures features)
{
     private Range m_range;

     static if (features == RangeFeatures.input)
     {
         auto front() @property { return m_range.front() }
     }
}


More information about the Digitalmars-d mailing list