[Issue 3821] writeln doesn't detect recursive data structures yet

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Dec 29 22:40:35 UTC 2019


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

--- Comment #11 from Steven Schveighoffer <schveiguy at yahoo.com> ---
It's possible, but at a huge cost. Just to see if you are printing infinitely
recursively, something that you may not expect to actually work. And also
impossible to know the state of the object, it could use things outside the
object to print (i.e. even though it's printing the same object, maybe it
terminates anyway).

Just don't print something that recurses infinitely.

Note that there's nothing stopping a type which MIGHT print recursively from
figuring this out in its toString function. e.g.:

struct List1
{
    int x;
    List1 *next;
    private bool printing = false;
    void toString(void delegate(const(char)[]) dg) const
    {
        if(printing)
        {
            dg("...");
            return;
        }
        printing = true;
        scope(exit) printing = false;
        import std.format;
        formattedWrite(dg, "%s ->", x);
        if(next is null)
            dg("null");
        else
            next.toString(dg);
    }
}

Solving it in format is the wrong answer, IMO.

--


More information about the Digitalmars-d-bugs mailing list