www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - __traits(isSame, TemplateOf ...) errors and some Idiomatic D questions

reply aliak <something something.com> writes:
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?
Jan 08
next sibling parent reply Seb <seb wilzba.ch> writes:
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`.
Jan 08
parent reply Seb <seb wilzba.ch> writes:
On Monday, 8 January 2018 at 23:14:32 UTC, Seb wrote:
 Your problem is that `TemplateOf!(int[])` isn't defined. It 
 should probably be changed to return `void`.
https://github.com/dlang/phobos/pull/6016
Jan 08
parent aliak <something something.com> writes:
On Monday, 8 January 2018 at 23:22:04 UTC, Seb wrote:
 On Monday, 8 January 2018 at 23:14:32 UTC, Seb wrote:
 Your problem is that `TemplateOf!(int[])` isn't defined. It 
 should probably be changed to return `void`.
https://github.com/dlang/phobos/pull/6016
Damn that's some fast turnaround! And thanks for the explanation as well :) Cheers
Jan 08
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Jan 08, 2018 at 10:59:44PM +0000, aliak via Digitalmars-d-learn wrote:
[...]
 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);
This seems unnecessarily complicated. What about this instead? enum r1Sorted = is(R1 : SortedRange!T, T...); Basically, what that is-expression means is: "Is R1 a template of the form `SortedRange!T` where T is some list of template arguments". Generally, using __traits directly is usually not recommended unless there's no other way to achieve what you want. T -- By understanding a machine-oriented language, the programmer will tend to use a much more efficient method; it is much closer to reality. -- D. Knuth
Jan 08
parent aliak <something something.com> writes:
On Monday, 8 January 2018 at 23:03:46 UTC, H. S. Teoh wrote:
 On Mon, Jan 08, 2018 at 10:59:44PM +0000, aliak via 
 Digitalmars-d-learn wrote: [...]
 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);
This seems unnecessarily complicated. What about this instead? enum r1Sorted = is(R1 : SortedRange!T, T...); Basically, what that is-expression means is: "Is R1 a template of the form `SortedRange!T` where T is some list of template arguments". Generally, using __traits directly is usually not recommended unless there's no other way to achieve what you want. T
Wow nice! Super nice :D Thanks! And ah I see, I suppose the double underscore kinda hints at that as well?
Jan 08