The Hidden toString/formatValue Recursion Trap - And How to Fix It

Forum User forumuser at example.com
Wed Mar 11 15:33:03 UTC 2026


On Wednesday, 11 March 2026 at 14:41:19 UTC, Aayush Patel wrote:
> **TL;DR**
> ```
> std.format's hasToString detection is broken for recursive 
> formatters. You must add:
> static if (__traits(compiles, { Writer w; w.put("test"); }))
> to every toString that calls formatValue. This is a design flaw 
> that needs fixing.
> ```
> Thoughts? Should I report this as a bug or enhancement request?

For a bug report you should

    1. Provide fully self-contained valid D code.
    2. Specify the compiler used and its version.
    3. Specify the expected and the obtained results
       from compilation and/or execution.

Your posting lacks all of these. After adding

    import std.stdio;

I could successfully compile with dmd v2.110.0 and GDC 12.1.0
and also run your code. The output is

     42

-> WORKSFORME -> no bug, no feature request.


More information about the Digitalmars-d-learn mailing list