Returning an empty range of a given type

rcorre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 13 19:47:22 PDT 2015


I've run into this situation a lot:
I have a function that returns a range (in this case, a slice of 
a custom container).
In some cases, the function needs to return an empty range.

It sounded like takeNone was what I wanted:

@nogc auto fun() {
    return (some_condition) ? getRange() : getRange.takeNone;
}

but there is a return type ambiguity. I finally ended up doing 
this:

@nogc auto fun() {
    return (some_condition) ? getRange().take(size_t.max) : 
getRange.takeNone;
}

I'm not sure if this is clever or insane.
It works, but just looks a bit crazy to me.
Does anyone else run into this situation? Have any cool ways to 
solve it?
MyRange is an inputRange, and I can't use a wrapper (InputRange) 
and keep the @nogc.


More information about the Digitalmars-d-learn mailing list