What does the $ operator actually do in EK9?

← Operators and Expressions · Ref: Q908

The $ operator in EK9 is the STRING CONVERSION operator. It calls the _string() method on any value and returns a String representation. That is ALL it does.

$ IS NOT STRING INTERPOLATION

In JavaScript and shell scripting, $variable inserts a value into a string. In EK9, $ only converts to String. String interpolation uses backtick strings with ${...} syntax.

$ IS NOT SAFE ACCESS

In Kotlin, $variable inside strings does interpolation. In TypeScript, $ has no special meaning. In EK9, $ is purely _string() conversion.

BASIC USAGE

  age <- 25
  ageText <- $age

This calls age._string() and stores the result "25" in ageText.

WITH DIFFERENT TYPES

  price <- 19.99
  priceText <- $price       converts Float to String
  flag <- true
  flagText <- $flag          converts Boolean to String
  name <- "Steve"
  nameText <- $name          identity on String (already String)

IN BACKTICK STRINGS

When you write `Age: ${$age}`, the outer ${...} is the interpolation syntax and the inner $ converts age to String. Both are needed when interpolating non-String values.

IMPLICIT CONVERSION

Inside backtick interpolation, the $ conversion is called implicitly if needed. But as a standalone operator, you must write $value explicitly.

CUSTOM TYPES

Any class can define operator $ to control its String representation. The operator must be pure and return String.

See Q897 for $ and #? together. See Q242 for all conversion operators. See Q909 for $ inside backtick strings.

Example

defines module qa.operators.dollarstringconv

  defines function

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

      // $ converts Integer to String via _string()
      ageText <- $age
      rtn: `Age is ${ageText}`

    describePrice() as pure
      -> price as Float
      <- rtn as String: String()

      // $ converts Float to String
      priceText <- $price
      rtn: `Price: ${priceText}`

  defines program

    DollarConversionDemo()
      stdout <- Stdout()

      // === $ converts Integer to String ===

      age <- 25
      ageText <- $age
      stdout.println(ageText)

      // === $ converts Float to String ===

      price <- 19.99
      priceText <- $price
      stdout.println(priceText)

      // === $ converts Boolean to String ===

      isActive <- true
      activeText <- $isActive
      stdout.println(activeText)

      // === Using functions that demonstrate $ ===

      stdout.println(describeAge(30))
      stdout.println(describePrice(9.99))

      // === $ inside backtick: ${variable} ===

      count <- 42
      stdout.println(`Count: ${count}`)

Common mistakes

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

Incorrect:

ageText <- "${age}"

Correct:

ageText <- $age
Other ways to ask this
  • Is $ string interpolation in EK9?
  • Does $ mean safe access in EK9?
  • How do I convert a value to String in EK9?

Coming from another language?

Java: toString() method call. Python: str() built-in function. JavaScript: String() or template literals with ${...}. Ruby: to_s method. Go: fmt.Sprintf or String() method. EK9: $value is a prefix operator that calls _string(), completely separate from backtick interpolation.

Keywords: conversion, operator, backtick, interpolation, prefix, string, _string, dollar