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
1
u/Tasty_Replacement_29 Bau May 23 '26
Looking at your language, I think the syntax looks consistent and nice. I think it is easy to understand, and I understand the decisions. A few things to consider:
A few general remarks:
forloop in your language. I see1.to(10).eachbut I have the feeling that this looks weird if you need the loop variable(s) in the block itself. I would consider porting a few examples from other languages to your language to compare the syntax. (Maybe you did that already, but I didn't see them... that's fine.) I tried analyzing larger codebases and see which concept is how frequent, and then see the most frequent concepts have the simplest syntax. Rare features can have more verbose syntax (as in data compression). Up to the point where you actually discourage a feature because it's hard to use.