Generating custom toString for structs

Paul Backus snarwin at gmail.com
Mon Jan 8 02:57:24 UTC 2024


On Sunday, 7 January 2024 at 09:49:36 UTC, Renato wrote:
> Is the above a "good" way to do this?

It looks ok to me. There are some minor changes I would make, 
like using `typeof(this)` instead of `S` to refer to the type of 
the struct you're mixing it into, but the overall approach is 
fine.

> Are there libraries (or even something in Phobos?) that already 
> provide some mixins or equivalent functionality?
>
> For reference, I wanted something like Java [Lombok's ToString 
> annotation](https://projectlombok.org/features/ToString), also 
> present in 
> [Groovy](https://docs.groovy-lang.org/latest/html/gapi/groovy/transform/ToString.html).

The [`boilerplate` package][1] has a [`GenerateToString` 
mixin][2] that looks pretty similar to the examples you linked.

[1]: https://code.dlang.org/packages/boilerplate
[2]: 
https://boilerplate.dpldocs.info/v1.9.1/boilerplate.autostring.GenerateToString.html


More information about the Digitalmars-d-learn mailing list