A problem with mutable toString

bearophile bearophileHUGS at lycos.com
Sun Apr 8 17:25:56 PDT 2012


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.059beta2:

[Foo]



While this gives errors:


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


Output, DMD 2.059beta2:

...\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(2559):        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(2559):        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(2559): 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



Is this good/expected?

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list