April 09, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7879

           Summary: writeln of struct array with not const toString()
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2012-04-09 13:15:55 PDT ---
Currently this code compiles and runs with no errors:

class Foo {
    override string toString() const {
        return "Foo";
    }
}
void main() {
    import std.stdio;
    const Foo[] foos = [new Foo];
    writeln(foos);
}


Output, DMD 2.059beta4:

[Foo]



While this code with not const Foo.toString:


class Foo {
    override string toString() /*const*/ {
        return "Foo";
    }
}
void main() {
    import std.stdio;
    const Foo[] foos = [new Foo];
    writeln(foos);
}


Gives:

...\dmd2\src\phobos\std\format.d(2158): Error: template instance
formatObject!(LockingTextWriter,const(Foo),char)
formatObject!(LockingTextWriter,const(Foo),char) does not match template
declaration formatObject(Writer,T,Char) if (hasToString!(T,Char))
...\dmd2\src\phobos\std\format.d(2158): Error: function expected before (), not
formatObject!(LockingTextWriter,const(Foo),char) of type void
...\dmd2\src\phobos\std\format.d(1840): Error: template instance
std.format.formatValue!(LockingTextWriter,const(Foo),char) error instantiating
...\dmd2\src\phobos\std\format.d(1579):        instantiated from here:
formatRange!(LockingTextWriter,const(Foo)[],char)
...\dmd2\src\phobos\std\format.d(2572):        instantiated from here:
formatValue!(LockingTextWriter,const(Foo)[],char)
...\dmd2\src\phobos\std\format.d(398):        instantiated from here:
formatGeneric!(LockingTextWriter,const(Foo[]),char)
...\dmd2\src\phobos\std\stdio.d(687):        ... (1 instantiations, -v to show)
...
...\dmd2\src\phobos\std\stdio.d(1574):        instantiated from here:
write!(const(Foo[]),char)
test.d(9):        instantiated from here: writeln!(const(Foo[]))
...\dmd2\src\phobos\std\format.d(1579): Error: template instance
std.format.formatRange!(LockingTextWriter,const(Foo)[],char) error
instantiating
...\dmd2\src\phobos\std\format.d(2572):        instantiated from here:
formatValue!(LockingTextWriter,const(Foo)[],char)
...\dmd2\src\phobos\std\format.d(398):        instantiated from here:
formatGeneric!(LockingTextWriter,const(Foo[]),char)
...\dmd2\src\phobos\std\stdio.d(687):        instantiated from here:
formattedWrite!(LockingTextWriter,char,const(Foo[]))
...\dmd2\src\phobos\std\stdio.d(1574):        instantiated from here:
write!(const(Foo[]),char)
test.d(9):        instantiated from here: writeln!(const(Foo[]))
...\dmd2\src\phobos\std\format.d(2572): Error: template instance
std.format.formatValue!(LockingTextWriter,const(Foo)[],char) error
instantiating
...\dmd2\src\phobos\std\format.d(398):        instantiated from here:
formatGeneric!(LockingTextWriter,const(Foo[]),char)
...\dmd2\src\phobos\std\stdio.d(687):        instantiated from here:
formattedWrite!(LockingTextWriter,char,const(Foo[]))
...\dmd2\src\phobos\std\stdio.d(1574):        instantiated from here:
write!(const(Foo[]),char)
test.d(9):        instantiated from here: writeln!(const(Foo[]))
...\dmd2\src\phobos\std\format.d(398): Error: template instance
std.format.formatGeneric!(LockingTextWriter,const(Foo[]),char) error
instantiating
...\dmd2\src\phobos\std\stdio.d(687):        instantiated from here:
formattedWrite!(LockingTextWriter,char,const(Foo[]))
...\dmd2\src\phobos\std\stdio.d(1574):        instantiated from here:
write!(const(Foo[]),char)
test.d(9):        instantiated from here: writeln!(const(Foo[]))
...\dmd2\src\phobos\std\stdio.d(687): Error: template instance
std.format.formattedWrite!(LockingTextWriter,char,const(Foo[])) error
instantiating
...\dmd2\src\phobos\std\stdio.d(1574):        instantiated from here:
write!(const(Foo[]),char)
test.d(9):        instantiated from here: writeln!(const(Foo[]))
...\dmd2\src\phobos\std\stdio.d(1574): Error: template instance
std.stdio.File.write!(const(Foo[]),char) error instantiating
test.d(9):        instantiated from here: writeln!(const(Foo[]))
test.d(9): Error: template instance std.stdio.writeln!(const(Foo[])) error
instantiating



I expect both to work and print correctly.
If this is not possible, then I'd like to see a single error message that asks
the programmer to add a missing "const" at line 2 :-)

Maybe related to the closed Issue 7864 ?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 25, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7879



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-25 11:25:49 PST ---
*** Issue 9325 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 25, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7879


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com
           Platform|x86                         |All
            Summary|writeln of struct array     |format of class with not
                   |with not const toString()   |const toString()
         OS/Version|Windows                     |All


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-25 11:26:57 PST ---
Renamed title because it's really a problem with Object.toString being non-const which causes the issue. (also the title mentions structs instead of classes).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Top | Discussion index | About this forum | D home