Bug 18486 – std.format cannot format const objects

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-02-21T18:09:41Z
Last change time
2018-02-28T00:52:44Z
Assigned to
No Owner
Creator
Jack Stouffer

Comments

Comment #0 by jack — 2018-02-21T18:09:41Z
======== import std.format; void main() { static class C {} const(C) c; format("%s", c); } ======== /dlang/dmd/linux/bin64/../../src/phobos/std/format.d(3570): Error: template instance std.format.formatObject!(Appender!string, const(C), char) does not match template declaration formatObject(Writer, T, Char)(ref Writer w, ref T val, ref const FormatSpec!Char f) if (hasToString!(T, Char)) /dlang/dmd/linux/bin64/../../src/phobos/std/format.d(1753): Error: template instance std.format.formatValueImpl!(Appender!string, const(C), char) error instantiating /dlang/dmd/linux/bin64/../../src/phobos/std/format.d(567): instantiated from here: formatValue!(Appender!string, const(C), char) /dlang/dmd/linux/bin64/../../src/phobos/std/format.d(5831): instantiated from here: formattedWrite!(Appender!string, char, const(C)) onlineapp.d(7): instantiated from here: format!(char, const(C))
Comment #1 by jack — 2018-02-21T18:53:19Z
hasToString returns 1 for C and 0 for const(C).
Comment #2 by jack — 2018-02-21T18:57:48Z
(In reply to Jack Stouffer from comment #1) > hasToString returns 1 for C and 0 for const(C). Oh geez, it's because Object.toString isn't marked const. *** This issue has been marked as a duplicate of issue 7879 ***
Comment #3 by github-bugzilla — 2018-02-28T00:52:44Z
Commit pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/93dc74be78de197d8c1daa52d9bd1920d07d7f47 Merge pull request #6207 from JackStouffer/issue18486 Fix Issue 7879 - format of const class with non const toString()