[Issue 4660] Different writeln output for lazy sequences

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Aug 15 10:54:52 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=4660

--- Comment #8 from hsteoh at quickfur.ath.cx ---
The point is that writeln treats both lazy and non-lazy ranges generically,
that's why it does not distinguish between them. If you really wanted to
distinguish between them, you could do this:

-----
string getFmtString(R)(R range)
    if (isInputRange!R)
{
    // Here we assume eager == array
    static if (is(R : A[], A))
        return "[%(%s, %)]";
    else
        return "[%(%s; %)]";
}

auto myRange = ...;
writefln(myRange.getFmtString, myRange); // this will do what you want,
generically
-----

Note that the above code is not 100% fool-proof; for example, R could be a
wrapper struct around an array, so the static if wouldn't identify it as an
eager range, but in fact it's actually eager. Or it could be a struct that
generates data in blocks of 500 elements each time -- would that qualify as
lazy or eager? Basically, there really isn't a 100% foolproof, generic way to
determine if something is eager or not. In fact, I don't know of a definition
of lazy vs. eager that covers 100% of the cases.

--


More information about the Digitalmars-d-bugs mailing list