Understanding isInfinite(Range)

Andrej Mitrovic andrej.mitrovich at test.com
Mon Sep 6 14:31:23 PDT 2010


That still won't work. Observe:

import std.stdio : writeln;

void main()
{
   writeln(isInputRange!(N));
}

class N
{
    N test;

    //~ bool empty()     // oops, we"re not an input range anymore
    //~ {
    //~     return false;
    //~ }

    @property
    void popFront()
    {
    }

    @property
    N front()
    {
        return test;
    }
}


template isInputRange(R)
{
    enum bool isInputRange = isValidCode!(
    {
        R r;                // can define a range object
        if (r.empty) {}     // can test for empty
        r.popFront;         // can invoke next
        auto h = r.front;   // can get the front of the range
    });
}

template isValidCode(alias code) { enum bool isValidCode = __traits(compiles, code); }

Instead of returning false, it will give out a compiler error.

Stanislav Blinov Wrote:

> 
> Andrej Mitrovic wrote:
> 
> > It does look nice. It would look even nicer if __traits gets renamed to meta.
> 
> By the way, there's no stopping writing
> 
> template isValidCode(alias code) { enum bool isValidCode = 
> __traits(compiles, code); }
> 
> :)



More information about the Digitalmars-d-learn mailing list