On Monday, 8 January 2018 at 22:59:44 UTC, aliak wrote:
Hi, trying to write some idiomatic generic D code and I'm a bit stuck with using the TemplateOf to check if a range is a SortedRange or not. A bit about the code, I'm basically rewriting https://dlang.org/library/std/algorithm/setops/set_difference.html but I want to do different things based on if the passed in ranges are sorted or not.

The error I'm getting is this:

onlineapp.d(61): Error: template std.traits.TemplateOf does not match any template declaration. And I use it like this:

enum r1Sorted = __traits(isSame, TemplateOf!(R1), SortedRange);

That's happening on line 61 and the weird thing is that I'm using TemplateOf exactly (i think) the same way on lines 28 and 29 and those do not error.

The code is here: https://run.dlang.io/is/9fowuP

If I substitute the isSame trait with compiles, then it works:

enum r1Sorted = __traits(compiles, TemplateOf!(R1), SortedRange);
pragma(msg, r1Sorted); // prints true

Any help would be appreciated! And on a side note, if anyone can point out any things I'm doing wrong when it comes to using D optimally/properly then please do.

Cheers! And thanks for any help!

PS:

1) Is there a better way to check if a predicate is unary or binary? 2) Is there an idiomatic way to check if a range is sortable or is it just "is(typeof(sort(range)))" basically it?

Why don't you use the `is` Expression?
https://wiki.dlang.org/Is_expression
It has the handy advantage that it will return false on compile errors.

is(TemplateOf!(typeof(r1)) == SortedRange);

With your example:

https://run.dlang.io/is/x2JWjI

Your problem is that `TemplateOf!(int[])` isn't defined. It should probably be changed to return `void`.

Reply via email to