Range.init does not imply Range.empty==true

Jonathan M Davis jmdavisProg at gmx.com
Thu Dec 20 12:58:22 PST 2012


On Thursday, December 20, 2012 12:48:10 H. S. Teoh wrote:
> I noticed that in some places in Phobos range-related code, there's an
> assumption that the following code works:
> 
> auto someRangeFunction(R)(R range) {
> ...
> R helperRange = range;
> ...
> helperRange = R.init;
> assert(helperRange.empty);
> ...
> }
> 
> Exercise for the reader: spot the bug.
> 
> Hint: what if R is an InputRangeObject?

takeNone should be used to empty a range - though whether you get the same 
type depends on the range (e.g. you'll get the result of takeExactly(range, 0) 
for an InputRangeObject). And it works the way that it does precisely because 
there's no guarantee that R.init is even usable. For instance, you can't use 
init with Voldemort types (which is the main reason that I seriously question 
the wisdom of Voldemort types at this point). However, any range which is a 
struct whose init value _isn't_ empty is wrong. We should be be able to rely 
on that at least.

- Jonathan M Davis


More information about the Digitalmars-d mailing list