Thread overview
[Issue 7637] New: writeln doesn't take custom toString into account
Mar 04, 2012
Andrej Mitrovic
Apr 19, 2012
SomeDude
Apr 19, 2012
Andrej Mitrovic
Apr 20, 2012
Kenji Hara
Apr 20, 2012
Andrej Mitrovic
March 04, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7637

           Summary: writeln doesn't take custom toString into account
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-03-03 17:11:49 PST ---
import std.stdio;

struct Foo {
    string toString(bool x) { return ""; }
}

void main()
{
    Foo foo;
    writeln(foo.toString());  // nogo: ok
    writeln(foo);  // compiles
}

The second writeln() call shouldn't compile. It seems writeln uses a default
toString if it doesn't find a toString() function that takes exactly zero
arguments.

Note that it will call the custom toString if 'x' has a default argument.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 19, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7637


SomeDude <lovelydear@mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear@mailmetrash.com


--- Comment #1 from SomeDude <lovelydear@mailmetrash.com> 2012-04-19 09:46:20 PDT ---
The above code doesn't compile on 2.059 Win32.

However this compiles:

import std.stdio;

struct Foo {
    string toString(bool x) { return ""; }
}

void main()
{
    Foo foo;
    writeln(foo.toString(true));  // nogo: ok
    writeln(foo);  // compiles
}



PS E:\DigitalMars\dmd2\samples> rdmd bug.d
toto
Foo()

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 19, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7637



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-04-19 10:11:26 PDT ---
(In reply to comment #1)
> The above code doesn't compile on 2.059 Win32.

nogo in a comment means that line doesn't compile. The second call is the interesting one.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7637



--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2012-04-20 03:11:31 PDT ---
This is current std.format design, not accepts-invalid bug.

Today, std.format.formatValue (it is internally used by writeln and writefln) only supports following toString specializations:

http://dlang.org/phobos/std_format.html
----
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);
const string toString();

If there is no toString() that matches required signatures, formatValue formats the object with 'TypeName(field-formattings-separeted-with-comma)', instead of reporting 'invalid toString signature detected' error.

There is two design decides:
- Ignoring invalid toString makes more user-defined types formattable.
- Shouldn't prevent any kind of toString that user really wants.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7637


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #4 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-04-20 09:19:13 PDT ---
Ok fair enough.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------