r/ProgrammingLanguages • u/oscarryz Yz • May 21 '26
String interpolation modes
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?
View all comments
Show parent comment
2
u/oscarryz Yz May 23 '26
If the syntax for a regular "object" is as follows (basically JSON-like)
Then replacing the `{` and `}` with back ticks, gets you infostrings that can be parsed, and validated exactly the same way
The plan is to use this object to let a compiler plugin augment the item, e.g. implement Debug, make it serializable, parse JSON, create a server config, or even crazier specify dependencies management instead of TOML, xml, yaml, .mod, .json, but I'm not sure if that last one is needed or even possible
By the way, I transcribed your Chess example 😄, I'm still far from having a complete compiler, but this is what it would look like (and actually I needed a large example)