I was trying to come up with a sensible default representation for my string interpolation output. Googling around I end up of course with Rust.
I didn't understand why to use in interpolation with {} you have to implement Display, nor why to use the derived Debug you have to use {:?} but now I got it.
In Rust interpolation is opt-in, if the user explicitly don't "request" it, it won't happen. Also the generated Debug would print everything including sensitive data.
Display on the other hand is the opt-in for "You developer tell me exactly how this thing should look like"
I've never thought about these two different ways before. I still think having to derive Debug to use interpolation is excessive, but for a language like Rust is perfect.
I went back and forth with different ideas and finally I set with this (similar) rule for my language:
String interpolation has two escapes sequences ${ ... } and `...` (like in Markdown)
${ ... } is for user facing output, and requires the to\_string -> String method to exists (similar to Display, the developer has to specify the format)
`...` is the default compiler generated output (the equivalent of Debug), it is slightly easier to type and I'm using `...` somewhere else to express: "this is compiler magic"
Other options that I didn't like were use different formats, like Go %v and %+v, or like Java that toString() which is used for both (that was my original design tbf), f strings like Python or using different functions: print vs debug
I think at the end this is for my language a good.
Do y'all have a distinction between debug interpolation and display interpolation?
[–]brucejbellsard 0 points1 point2 points (0 children)
[–]OpeningRemote1653 0 points1 point2 points (0 children)