www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7637] New: writeln doesn't take custom toString into account

reply d-bugmail puremagic.com writes:
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



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: -------
Mar 03 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7637


SomeDude <lovelydear mailmetrash.com> changed:

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



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: -------
Apr 19 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7637




10:11:26 PDT ---

 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: -------
Apr 19 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7637




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: -------
Apr 20 2012
prev sibling parent d-bugmail puremagic.com writes:
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



09:19:13 PDT ---
Ok fair enough.

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