View mode: basic / threaded / horizontal-split · Log in · Help
April 09, 2012
A problem with mutable toString
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
April 09, 2012
Re: A problem with mutable toString
Maybe related to this?
http://d.puremagic.com/issues/show_bug.cgi?id=7864
April 10, 2012
Re: A problem with mutable toString
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:
>
[snip]

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
Top | Discussion index | About this forum | D home