tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

56 lignes · 4305 octetsBrut · Historique

ADR 0034: String interpolation through Show

Status: Proposed (2026-10-13, issue #152); needs engineering review (#44).

Context

  • Building a string from values is verbose: show(x) and <> everywhere, as in "listening on " <> show(net.port(server)). There were about 167 show(..) <> sites across the examples, std and tests.

  • The question asked: could a type that derives Show be used where a String is expected? That is an implicit Show ⇒ String conversion, a subtyping edge in Hindley–Milner. When inference unifies String with an unknown α, it can't tell whether α is a String or a value to convert:

    • types stop being principal;
    • results depend on annotations and checking order;
    • Show<α> constraints leak into inferred signatures.

    Rust, Haskell, Gleam, Elm and Koka all avoid it. Louise already keeps its one coercion, Never, out of unification (ADR 0007).

Decision

No implicit conversion. Every string literal interpolates instead:

fn report :: server: Server request: Request elapsed: Int -> () / <io, log> {
  io.println "listening on {net.port server}"
  log "got {request.method} {request.path} in {elapsed}ms"
}

fn check :: expected: List<Int> actual: List<Int> -> () {
  assert (expected == actual) "expected {expected:?}, got {actual:?}"
}
  • {e} is show(e); {e:?} is debug(e). The trait methods are the prelude's Show and Debug, whatever the names show and debug mean where the string is (they may be shadowed). Show is the user-facing text, and show on a String returns it unchanged, so "hello {name}" inserts the name as is. Debug quotes strings and names record fields.
  • A hole holds any expression, including calls, operators, field paths and string literals of its own: "{list.length(xs)} items", "{if ok { "yes" } else { "no" }}".
  • Typing: each hole adds a Show<τ> (or Debug<τ>) constraint on its expression's type, exactly as the call show(e) would. Inference is unaffected: the literal's type is String.
  • Evaluation: holes are evaluated left to right, and the parts are joined in one allocation (the runtime's tl_string_join, as string.concat), not by a chain of <>.
  • Escapes: \{ and \} are literal braces. An unescaped } outside a hole is an error, so a stray brace is caught rather than kept. 4 existing literals had braces and were escaped.
  • HTML literals (ADR 0009): a text hole, as in <td>{user.age}</td>, now takes any type with Show as well as String and Html. The value is shown, then escaped like any text. Html values are still inserted unescaped, and attributes keep their rules (E0601).

Why {e}:

  • HTML literals already write holes this way;
  • braces were rare in string literals.

The cost falls on text that contains braces, JSON or CSS kept in strings, which must write \{. #108 (CSS) is expected to use its own literal or builder rather than plain strings.

Tooling:

  • the formatter leaves string text untouched and formats a hole's expression as code;
  • the tree-sitter grammar parses holes as expressions, so editors highlight them as code;
  • the language server gives hover and go-to-definition inside holes.

Consequences

  • Shorter code for messages, logs and tests, and one allocation instead of one per <>.
  • A literal brace needs a backslash. A stray { in an old string becomes a hole, usually a type error, so the change is loud.
  • No new trait. Show is the display trait and Debug the developer view, as before.

Alternatives considered

  • An implicit Show ⇒ String conversion, rejected above.
  • ${e} (JS, Kotlin) or \(e) (Swift): no clash with JSON or CSS text, but unlike HTML literals, and noisier for the common case.
  • A prefixed literal ($"…{e}…"): plain strings would never change meaning, but it's one more literal form to learn. The owner chose interpolation in every string.
  • Holes limited to names and field paths: simpler to scan, but forces a let for every call. Any expression it is, as in Rust 2021's inline arguments plus Swift's and Kotlin's expressions.
  • Only the HTML change (the issue's lighter alternative): done too, but alone it doesn't help logs, errors and plain-text responses.