www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A problem with mutable toString

reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Apr 08 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Maybe related to this?
http://d.puremagic.com/issues/show_bug.cgi?id=7864
Apr 09 2012
prev sibling parent "Kenji Hara" <k.hara.pg gmail.com> writes:
On Monday, 9 April 2012 at 00:25:57 UTC, bearophile wrote:
 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:

Current std.format doesn't support const class formatting with upcasting to mutable Object and using mutable toString().
 Is this good/expected?

It is a gap between language const system and object.Object class interface. I think Phobos should not break const system in basic. Kenji Hara
Apr 09 2012