Bug 24610 – Basic range-based `toString` not recognized

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2024-06-16T08:58:52Z
Last change time
2024-06-16T10:34:38Z
Assigned to
No Owner
Creator
Max Samukha

Comments

Comment #0 by maxsamukha — 2024-06-16T08:58:52Z
import std.array; import std.format; struct S { void toString(Output)(ref Output output) { import std.range: put; output.put("S"); } } void main() { S s; auto output = appender!(char[]); s.toString(output); assert(output[] == "S"); // ok output.clear(); formattedWrite!"%s"(output, s); import std.stdio; assert(output[] == "S"); // fail } The `toString` is implemented according to the spec (https://dlang.org/phobos/std_format_write.html), and the formatter should use it.
Comment #1 by maxsamukha — 2024-06-16T09:38:27Z
`import std.stdio;` is unnecessary.
Comment #2 by maxsamukha — 2024-06-16T10:34:38Z
Ok, my bad. The range wants a char, not string. (Anyway, nothing is more annoying than the silent acceptance of malformed toString.)