Bug 7879 – format of class with not const toString()
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-04-09T13:15:55Z
Last change time
2018-02-28T00:52:45Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-04-09T13:15:55Z
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 ?
Comment #1 by andrej.mitrovich — 2013-01-25T11:25:49Z
*** Issue 9325 has been marked as a duplicate of this issue. ***
Comment #2 by andrej.mitrovich — 2013-01-25T11:26:57Z
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).
Comment #3 by jack — 2018-02-21T18:57:48Z
*** Issue 18486 has been marked as a duplicate of this issue. ***