www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17420] New: std.format.formatObject doesn't work with safe

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

          Issue ID: 17420
           Summary: std.format.formatObject doesn't work with  safe
                    toString
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: jack jackstouffer.com

In order to speed up formatting, std.format allows for the following overloads
of toString to be defined


    const void toString(scope void delegate(const(char)[]) sink, FormatSpec
fmt);
    const void toString(scope void delegate(const(char)[]) sink, string fmt);
    const void toString(scope void delegate(const(char)[]) sink);

This allows std format to skip an allocation because the output string of the
object/struct is written directly to the writer.

The problem is that formatObject doesn't work with any of the common function
attributes `pure  safe  nogc nothrow`. If the toString overload is marked with
any of these, hasToString!(YourType) == 0 or it skips your overload and uses
the allocating version.

Either

1. hasToString needs to be modified to recognize these overloads if it's
possible to make the existing format code call safe code
2. Failing that, a couple of new specializations of toString should be defined
that is more idiomatic of Phobos that gives the function an OutputRange
directly, like so

    const void toString(O)(O sink, FormatSpec fmt) if (isOutputRange!(O,
char));
    const void toString(O)(O sink, string fmt) if (isOutputRange!(O, char));
    const void toString(O)(O sink) if (isOutputRange!(O, char));

--
May 23