moonbit-docs icon indicating copy to clipboard operation
moonbit-docs copied to clipboard

[Suggestion] Allow string literals (e.g., in map["key"]) within interpolation expressions

Open PatrickkZhao opened this issue 8 months ago • 2 comments

Motivation / Problem

Currently, the official documentation states that string interpolation expressions (\{...}) cannot contain double quotes (").

This is a reasonable limitation, but it makes a very common and intuitive programming pattern—interpolating a map value accessed by a string key—feel cumbersome. To achieve this, developers must create a temporary variable, which adds boilerplate code and makes the intent less direct.

For example, this highly intuitive code is currently disallowed:

// This code feels natural, but fails due to the `"` limitation.
fn main() {
  let my_map = Map::of([("key", 123)])
  println("The value is: \{my_map["key"]}") 
}

The required workaround is to use a temporary variable, which is less ergonomic:

// This works, but is less direct.
fn main() {
  let my_map = Map::of([("key", 123)])
  let value = my_map["key"]
  println("The value is: \{value}")
}

PatrickkZhao avatar Jun 24 '25 07:06 PatrickkZhao

try.moonbitlang.com/#7b99eeb7

PatrickkZhao avatar Jun 24 '25 07:06 PatrickkZhao

This is a reasonable limitation, but it makes a very common and intuitive programming pattern—interpolating a map value accessed by a string key—feel cumbersome.

Allowing " in the interpolation will complicate the lexer and error recovery in the parser, but it is a compelling argument. @bobzhang

Yoorkin avatar Jul 14 '25 07:07 Yoorkin