is operator and SortedRange
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Nov 11 04:02:10 PST 2016
On Friday, 11 November 2016 at 11:49:25 UTC, RazvanN wrote:
> I am a bit confused about how the is operator works. I have a
> function which receives an InputRange and a predicate. Now I
> need to be able to test if the InputRange is actually a
> SortedRange. I don't care about how the datatypes behind the
> SortedRange or the predicate, I just need to see if the object
> is a SortedRange. I have tried the following test:
>
> static if(is(typeof(haystack) == SortedRange!(T, _pred), T,
> _pred))
>
> where haystack is the InputRange, but the test fails. Is there
> a way to test if the InputRange is a SortedRange without having
> to explicitly pass the primitive tupe on top of which the
> SortedRange is built?
template isSortedRange(T) {
private import std.range : SortedRange;
static if (is(T : SortedRange!TT, TT)) {
enum isSortedRange = true;
} else {
enum isSortedRange = false;
}
}
void main () {
import std.algorithm : sort;
int[] a;
a ~= [1, 6, 3];
auto b = a.sort;
pragma(msg, typeof(b));
pragma(msg, isSortedRange!(typeof(a))); // false
pragma(msg, isSortedRange!(typeof(b))); // true
}
More information about the Digitalmars-d-learn
mailing list