digitalmars.D.learn - is operator and SortedRange
- RazvanN (12/12) Nov 11 2016 I am a bit confused about how the is operator works. I have a
- Jonathan M Davis via Digitalmars-d-learn (20/32) Nov 11 2016 The correct way to do this with an is expression is a bit esoteric, and ...
- ketmar (18/30) Nov 11 2016 template isSortedRange(T) {
- RazvanN (2/21) Nov 11 2016 Thank you! Worked like a charm
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?
Nov 11 2016
On Friday, November 11, 2016 11:49:25 RazvanN via Digitalmars-d-learn 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?The correct way to do this with an is expression is a bit esoteric, and you don't want to have to deal with it. Fortunately, someone added an appropriate trait to std.traits to do this for you: std.traits.isInstanceOf (which should arguably be called isInstantiationOf, but it is what it is): http://dlang.org/phobos/std_traits.html#isInstanceOf And by the way, what you're trying to use is an "is expression," not the "is operator." The is operator is used for doing a bitwise comparison of two objects. e.g. struct S { int i; } assert(S(5) is S(5)); or int[] arr; assert(arr is null); and it's a runtime constructor, not a compile time one, unlike is expressions. - Jonathan M Davis
Nov 11 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 }
Nov 11 2016
On Friday, 11 November 2016 at 12:02:10 UTC, ketmar wrote:On Friday, 11 November 2016 at 11:49:25 UTC, RazvanN wrote:Thank you! Worked like a charm[...]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 }
Nov 11 2016