Is $ string interpolation or safe access in EK9?

← Operators and Expressions · Ref: Q918

No. $ does NOT do string interpolation. $ does NOT do safe access. $ converts ANY value to String by calling _string(). That is the ONLY thing it does.

WHAT $ DOES

$ is a PREFIX operator that calls _string() on any value and returns a String:

  age <- 25
  ageText <- $age         calls Integer._string(), returns "25"
  price <- 19.99
  priceText <- $price     calls Float._string(), returns "19.99"
  flag <- true
  flagText <- $flag       calls Boolean._string(), returns "true"

WHAT $ DOES NOT DO

1. NOT string interpolation — that is backtick ${...}
2. NOT safe access — use ? suffix for isSet checks
3. NOT variable sigil — EK9 variables have no sigils
4. NOT template literal marker — backtick strings handle that
5. NOT currency symbol — it is an operator

COMMON CONFUSION WITH BACKTICK STRINGS

Inside backtick strings:

  `Hello ${name}`      ${...} is interpolation, not $ operator
  `Age: ${$age}`       inner $ is the operator, outer ${} is interpolation

Standalone:

  text <- $value         this IS the $ operator (conversion)
  msg <- "${value}"     this does NOT interpolate (double-quoted string)

SAFE ACCESS USES ?

For checking if a value is set, use the ? suffix operator:

  if value?
    process(value)

$ has nothing to do with null safety or set checking.

See Q908 for $ basics. See Q909 for $ in backtick strings. See Q898 for ? isSet operator.

Example

defines module qa.operators.dollarnotinterp

  defines function

    // $ converts to String, nothing else
    convertAll() as pure
      ->
        number as Integer
        ratio as Float
        isEnabled as Boolean
      <- rtn as String: String()

      numText <- $number
      ratioText <- $ratio
      enabledText <- $isEnabled
      rtn: `${numText}, ${ratioText}, ${enabledText}`

  defines program

    DollarNotInterpDemo()
      stdout <- Stdout()

      // === $ is _string() conversion ===

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

      // === $ is NOT interpolation ===
      // Interpolation uses backtick ${...}

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

      // === $ inside backtick is conversion ===

      score <- 100
      stdout.println(`Score: ${score}`)

      // === ? is isSet, NOT $ ===

      count <- 42
      stdout.println(`Is set: ${count?}`)

      // === Multiple conversions ===

      stdout.println(convertAll(10, 3.14, true))

      // === $ on already-String is identity ===

      greeting <- "World"
      greetText <- $greeting
      stdout.println(`Greeting: ${greetText}`)

Common mistakes

E08090 — Double-quoted strings do NOT support interpolation — '${age}' is literal text, so the 'age' variable becomes unreferenced. Use backtick strings for interpolation or $ as a prefix operator. See ek9 -h E08090 for details.

Incorrect:

ageText <- "Age: ${age}"

Correct:

ageText <- $age
Other ways to ask this
  • Does $ do interpolation in EK9?
  • Is $ a safe access operator in EK9?
  • What does $ NOT do in EK9?

Coming from another language?

JavaScript: $ in template literals ${expr}. Kotlin: $variable in strings for interpolation. PHP: $variable as variable sigil. Perl: $scalar as variable sigil. Shell: $VAR for variable expansion. EK9: $ is ONLY a prefix operator calling _string(), never interpolation, never variable sigil, never safe access.

Keywords: interpolation, not, safe, access, conversion, prefix, string, operator, dollar, misconception