Can you explain this?

Colin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 20 13:01:03 PDT 2014


Hi,

I'm implementing some template checks on some types I'm using in 
a project, and went to phobos for some indications on how to use 
them.

In std.range, I see this construct quite a bit:

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
     }));
}

Can anyone explain the:
  is(typeof(
     (inout int = 0) {}
   );

section to me?

It looks very....hacky.

I see 3 distinct parts playing a role in my confusion:
A) The 'is' keyword. What does it do when you have is(expression);
B) typeof( expression ); whats this doing? Particularly when the 
expression its acting on is a closure that returns nothing? (at 
least as far as I can see)
C) The closure expression:
(inout int = 0) {
    // Check to see if I can do InputRangy stuff...
}
Why is there a need for that inout int = 0 clause at the start of 
it?

Sorry for the long question!

Thanks,
Colin


More information about the Digitalmars-d-learn mailing list