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?
5
u/OpeningRemote1653 May 22 '26
Most languages don't make a hard distinction. Python's f-strings and Java's
toString()blur the two concerns into one, while C's printf-style formatting separates them by format specifier but not by intent. Rust is a exception with itsDisplayvsDebugtrait split, and your design echoes that nicely. Swift hasCustomStringConvertible(for display) andCustomDebugStringConvertible(for debug) as separate protocols, but both are accessed via the same interpolation syntax, so the distinction is softer. Kotlin similarly hastoString()for everything and leans ondata classauto-generated representations for debug-like output.I'd lean toward Rust's explicit separation. The Java/Python "one method for everything" approach always felt like it was optimizing convenience at the cost of accidentally leaking representation into user-facing output.
3
u/Tasty_Replacement_29 Bau May 22 '26 edited May 22 '26
I think it is very useful to have a distinction. In Java I often usually toString() for debug output. And for "display" (or rather, for all kinds of other processing) I tend to use different methods, like "format()", "toHTML()", etc. BTW not sure if you are familiar with the (stopped) "String template" effort in Java.
In my language, to reduce complexity, there is no string interpolation. Instead this is done via var-args with optional commas. Example:
This is the same as:
... but commas are often in the way, so I made them optional in the simple cases. I think this works quite well (for how simple it is). The arguments are then converted to an array of the expected type. For println, that is a string.
For debug logging, I use a macro system, a bit like Rust, except it's simpler. Example:
This is useful if you want to have debug output, but it can be used for other things like assertions, regular logging, SQL code generation, map / filter, serialization etc. In the above example, "debug" is a macro that can print for example:
The macro not only has access to the value, but also to the AST / source code and type etc. Like in Rust. Such a macro system is quite powerful in my view; it's not only about string interpolation, but also about compile time processing of things etc (metaprograming).
3
u/brucejbell sard May 21 '26 edited May 21 '26
For my project, I have a Display-like
#ToStrtrait to specify a default format, but I devolve everything else to explicit methods:Trait
#ToStris required to label the.strmethod as the default format. We also add a.dbgmethod which is not affiliated with any trait.Instead of special formatting syntax, the standard types provide short formatting methods. Likewise, if you want a specific
.dbgformat just declare it as a method, no trait is necessary.[disclaimer: I still don't have my implementation up, the above is aspirational...]
3
u/bcardiff May 22 '26
In Ruby and Crystal the distinction is to_s / inspect methods for string representation.
But if you are designing the interpolation I would suggest to validate how an html template could be supported.
Does it support safe interpolation by default or will the user need to escape? eg:
”<p>#{name}<p>”vs”<p>#{html(name)}<p>”Can it be extended to other view languages?
3
u/renozyx May 22 '26
Display printing can(should?) be I18n aware while usually debug printing isn't.
1
u/dnpetrov May 23 '26
Python has `!s` and `!r` in f-strings. They boil down to different method calls (`__str__` and `__repr__`), where `__str__` should produce human-readable output (akin to Display in Rust), and `__repr__` should produce non-ambiguous representation (kinda like Debug in Rust). Rust provides "extension points" via predefined traits, Python does that with dunder methods. So, from the point of language design, in both cases these are just two extension points: one for human-readable output, one for debugging output.