how does isInputRange(T) actually work?

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 21 12:42:40 PDT 2015


On Tuesday, 21 April 2015 at 19:17:56 UTC, kevin wrote:
> On Tuesday, 21 April 2015 at 19:13:34 UTC, Meta wrote:
>> On Tuesday, 21 April 2015 at 19:11:43 UTC, John Colvin wrote:
>>> On Tuesday, 21 April 2015 at 19:06:39 UTC, kevin wrote:
>>>> 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
>>>>  }));
[...]
> Don't lambdas need a => token?

No, D has two variants of function/delegate literal or lambda 
syntax:

1) with "=>": parameters => expression
Some examples:
(int a) => a + 2
a => a + 2
(a, b) => a + b
() => 2

2) with braces: (parameters) {statements}
Same examples as above:
(int a) {return a + 2;}
(a) {return a + 2;}
(a, b) {return a + b;}
{return 2;}

As you can see, a lot is optional there.

In the spec: http://dlang.org/expression.html#FunctionLiteral

> Also, what is the purpose of typeof? I would have expected a 
> simple is() to work just fine.

(In this most simple form,) `is` evaluates to true if the 
argument is a valid type. A function/delegate literal isn't a 
type.

If you passed the lambda expression itself to `is`, the result 
would always be false. As it is, the result is true when the 
lambda expression compiles (so it has a valid type).

More about the IsExpression: 
http://dlang.org/expression.html#IsExpression


More information about the Digitalmars-d-learn mailing list