www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Named template constraints

reply Tim Holzschuh via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Hi there,

I recently read the 'More Templates' chapter of Ali's book (<-- thanks 
for that ;) ).
At the section 'Named constraints', there were a definition like this:

template isUsable(T)
{
     enum isUsable = is ( typeof(
     {
         T obj;
         obj.call();
         obj.otherCall(1);
         obj.ye tAnotherCall();
     }() ) );
}

But at Phobos I always see definitions like this (std.range : isInputRange):

template isInputRange(R)
{
enum bool isInputRange = is(typeof(
(inout int = 0)
{
R r = R.init; // can define a range object
if (r.empty) {} // can test for empty
r.popFront(); // can invoke popFront()
auto h = r.front; // can get the front of the range
}));
} What does (inout int = 0) mean/affect here? I created the same 
template for myself just without the (inout int = 0) and it worked (at 
least with a dummy struct).. - Tim
Apr 22 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/22/2014 07:07 AM, Tim Holzschuh via Digitalmars-d-learn wrote:

 read the 'More Templates' chapter of Ali's book (<-- thanks
 for that ;) ).
Yay! :)
 At the section 'Named constraints', there were a definition like this:

 template isUsable(T)
 {
      enum isUsable = is ( typeof(
      {
          T obj;
          obj.call();
          obj.otherCall(1);
          obj.ye tAnotherCall();
      }() ) );
 }
That is how Phobos used to be.
 But at Phobos I always see definitions like this (std.range :
 isInputRange):

 template isInputRange(R)
 {
 enum bool isInputRange = is(typeof(
 (inout int = 0)
I had noticed that as well and am curious myself. My guess is that it is a syntax issue where that default parameter list makes it a stronger delegate syntax. :p
 {
 R r = R.init; // can define a range object
 if (r.empty) {} // can test for empty
 r.popFront(); // can invoke popFront()
 auto h = r.front; // can get the front of the range
 }));
 } What does (inout int = 0) mean/affect here? I created the same
 template for myself just without the (inout int = 0) and it worked (at
 least with a dummy struct).. - Tim
Same here: I cannot find an example where the first version does not work. (?) Ali
Apr 22 2014