how does isInputRange(T) actually work?
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Apr 21 12:35:04 PDT 2015
On 4/21/15 3:11 PM, 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
>> }));
>>
>>
>> ... is the current implementation in Phobos. But I can't seem to
>> understand this syntax. What is (inout int = 0)? Why can a block
>> follow it?
>>
>> My guess is that this is declaring some sort of function and testing
>> if it is syntactically valid, but this is still strange to me.
>
> It's defining a lambda function and checking that it is *semantically*
> valid.
>
> No idea what the `(inout int = 0)` is there for, I would have thought it
> would be fine without it.
inout has a rule that you can't declare a type of inout as a local
variable unless there is an inout parameter/return. I'm not sure if this
rule still exists (it causes weird shit like the above to be required).
I know some inout rules that I devised have been relaxed for the benefit
of generic function sanity.
The inout int = 0 gives the lambda an inout parameter, in case you are
doing isInputRange!(inout(int)[]), then you can validly declare a range
of type R inside your function.
Alternatively, you could have the lambda take an R as a parameter. Or
fix the semantics so that inout local variable acts like immutable
inside a non-inout function.
-Steve
More information about the Digitalmars-d-learn
mailing list