r/ProgrammingLanguages • 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?

11 Upvotes

11 comments sorted by

View all comments

Show parent comment

2

u/oscarryz Yz May 23 '26
  • Yes, someone mentioned that too, I need to add more examples and validations for the string interpolation.
  • Keywords and The @ language. The @ lang looks so cool, but I'm biased of course. Yes, this lack of keywords for the sake of it has been bitten back a couple of times. The most important is it gets really hard to read ( cough lisp...) because everything blends together. I went back and forth several times until I added different symbols for different meanings, which is still hard to read, e.g. [ ] for things that vary (arrays and hashmaps) #( ) for things that define aka. signatures / interfaces, and now ` ` for things compiler (and { } of course for everything else). Unfortunately I'm too deep into this, so maybe for the next one, but I agree, it hurts readability.
  • Infostrings (need a better name). Indeed realized infostrings would need it's own meta language to be useful, but then I realized a regular "object" is enough.

If the syntax for a regular "object" is as follows (basically JSON-like)

{
   array: [1,2,3]
   something : "hola"
   nested: {
      bar : false
   }
}

Then replacing the `{` and `}` with back ticks, gets you infostrings that can be parsed, and validated exactly the same way

`
array: [1,2,3]
something : "hola"
nested: {
   bar : false
}
`
Employee: {...}

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

`
dependencies: [
  { name: "serde";  version : "1.0" ; features = ["derive"] },
  { name:  "serde_json"; version: "1.0"}
]
MyProgram: { ... }
`
  • for loop. Yes, 1.to(10).do(block) will do block 10 times, 1.to(10).each( index, block) will make the current index available. For all others you would use while which is a recursive function with a condition block + body block, it would need the indices declared outside
i: 0
j: some.len()
while({ i > j}, {
   // use i,j
   i.++()
   j.--()
})

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)