How does $ work inside backtick strings in EK9?

← Operators and Expressions · Ref: Q909

In EK9 backtick strings, there are TWO separate mechanisms at work. Understanding the difference is critical.

BACKTICK INTERPOLATION: ${...}

The ${...} syntax inside backtick strings is the INTERPOLATION mechanism. It evaluates the expression inside and inserts the result into the string. This ONLY works in backtick strings, never in double-quoted strings.

DOLLAR CONVERSION: $

The $ operator converts any value to String by calling _string(). It is a PREFIX operator used outside or inside interpolation.

COMBINED: ${$variable}
When you write `Age: ${$age}`, two things happen:
1. The inner $age calls age._string() to convert Integer to String
2. The outer ${...} interpolates that String result into the backtick string

WHEN $ IS OPTIONAL INSIDE ${}

For String variables, you can write ${name} without the inner $ because the value is already a String. For non-String types (Integer, Float, Boolean, custom types), the $ conversion happens implicitly inside ${} interpolation.

EXAMPLES

  name <- "Steve"
  stdout.println(`Hello ${name}`)       String, no $ needed
  age <- 25
  stdout.println(`Age: ${$age}`)        Integer, $ converts
  stdout.println(`Age: ${age}`)         also works, implicit conversion

DOUBLE-QUOTED STRINGS

Double-quoted strings like "Hello ${name}" do NOT support interpolation. The ${...} is treated as literal text. Always use backtick strings for interpolation.

See Q908 for $ as standalone operator. See Q242 for all conversion operators. See Q897 for operator overview.

Example

defines module qa.operators.dollarbacktick

  defines function

    formatPerson() as pure
      ->
        name as String
        age as Integer
      <- rtn as String: String()

      // String variable: ${name} works directly
      // Integer variable: ${age} uses $ to convert
      rtn: `${name} is ${age} years old`

  defines program

    DollarBacktickDemo()
      stdout <- Stdout()

      // === String inside backtick — no $ needed ===

      name <- "Steve"
      stdout.println(`Hello ${name}`)

      // === Integer inside backtick — $ converts ===

      age <- 25
      stdout.println(`Age: ${age}`)

      // === Float inside backtick ===

      price <- 19.99
      stdout.println(`Price: ${price}`)

      // === Boolean inside backtick ===

      isEnabled <- true
      stdout.println(`Active: ${isEnabled}`)

      // === Multiple values ===

      stdout.println(formatPerson("Alice", 30))

      // === Expression inside backtick ===

      x <- 10
      y <- 20
      stdout.println(`Sum: ${(x + y)}`)

Common mistakes

E08090 — Double-quoted strings do NOT interpolate, so '${age}' is literal text and 'age' becomes unreferenced; use backtick strings for interpolation. See ek9 -h E08090 for details.

Incorrect:

      stdout.println("Age: ${age}")

Correct:

      stdout.println(`Age: ${age}`)
Other ways to ask this
  • What does ${$variable} mean in EK9?
  • How do I interpolate non-String values in EK9 backtick strings?
  • Why do I need $ inside ${} in EK9?

Coming from another language?

Java: String.format() or + concatenation, no interpolation. Python: f-strings with {variable}. JavaScript: template literals with ${expression}. Kotlin: "$variable" or "${expression}". Ruby: "#{expression}". EK9: backtick strings with ${expression}, $ operator for explicit conversion, double-quoted strings have NO interpolation.

Keywords: conversion, dollar, backtick, string, template, expression, interpolation