Search code examples
drangephobos

How do you use ranges in D?


Whenever I try to use ranges in D, I fail miserably.

What is the proper way to use ranges in D? (See inline comments for my confusion.)

void print(R)(/* ref? auto ref? neither? */ R r)
{
    foreach (x; r)
    {
        writeln(x);
    }

    // Million $$$ question:
    //
    // Will I get back the same things as last time?
    // Do I have to check for this every time?

    foreach (x; r)
    {
        writeln(x);
    }
}

void test2(alias F, R)(/* ref/auto ref? */ R items)
{
    // Will it consume items?
    // _Should_ it consume items?
    // Will the caller be affected? How do I know?
    // Am I supposed to?
    F(items);
}

Solution

  • Sorry, I can't fit this into a comment :D. Consider if Range were defined this way:

    interface Range {
        void doForeach(void delegate() myDel);
    }
    

    And your function looked like this:

    void myFunc(Range r) {
        doForeach(() {
            //blah
        });
    }
    

    You wouldn't expect anything strange to happen when you reassigned r, nor would you expect to be able to modify the caller's Range. I think the problem is that you are expecting your template function to be able to account for all of the variation in range types, while still taking advantage of the specialization. That doesn't work. You can apply a contract to the template to take advantage of the specialization, or use only the general functionality. Does this help at all?

    Edit (what we've been talking about in comments):

    void funcThatDoesntRuinYourRanges(R)(R r)
    if (isForwardRange(r)) {
        //do some stuff
    }
    

    Edit 2 std.range It looks like isForwardRange simply checks whether save is defined, and save is just a primitive that makes a sort of un-linked copy of the range. The docs specify that save is not defined for e.g. files and sockets.