String Interpolation

Part of: Reef Language Reference Last reviewed on version: 0.8.0 Status: Implemented


Overview

Reef supports string interpolation for embedding variable values directly into string literals using the ${variable} syntax.


Basic Syntax

let name = "Alice"
let greeting = "Hello, ${name}!"
println(greeting)  // Output: Hello, Alice!

Simple Variable Interpolation

Only simple variable names are supported inside ${}:

let x = 42
let y = 3.14
let s = "world"

println("x = ${x}")           // x = 42
println("y = ${y}")           // y = 3.14
println("Hello, ${s}!")       // Hello, world!

Multiple Interpolations

Multiple variables can be interpolated in a single string:

let first = "John"
let last = "Doe"
let age = 30

println("Name: ${first} ${last}, Age: ${age}")
// Output: Name: John Doe, Age: 30

Escaping Dollar Signs

To include a literal $ character, use \$:

println("Price: \$100")       // Output: Price: $100
println("Variable: \${name}") // Output: Variable: ${name}

String Escape Sequences

Within interpolated strings, these escape sequences work:

Escape Meaning
\n Newline
\t Tab
\r Carriage return
\\ Backslash
\" Double quote
\$ Dollar sign

Multi-Line Strings ✅ NEW

Reef supports multi-line strings using triple quotes ("""). These strings:

  • Preserve newlines
  • Support automatic dedentation (leading whitespace is stripped based on the closing """)
  • Support string interpolation with ${variable}

Basic Multi-Line String

let text = """
    Hello, World!
    This is a multi-line string.
    Each line is preserved.
"""
println(text)

Output:

Hello, World!
This is a multi-line string.
Each line is preserved.

Automatic Dedentation

The compiler automatically removes common leading whitespace:

proc main()
    let message = """
        Line 1
        Line 2
        Line 3
    """
    // Leading spaces are stripped based on the indentation
end main

Multi-Line with Interpolation

String interpolation works inside multi-line strings:

let name = "Reef"
let version = "1.2"

let banner = """
    Welcome to ${name}!
    Version: ${version}
    Enjoy coding!
"""
println(banner)

Output:

Welcome to Reef!
Version: 1.2
Enjoy coding!

Escape Sequences in Multi-Line Strings

All escape sequences work in multi-line strings:

let text = """
    Tab:\there
    Quote:\"hello\"
    Dollar:\$100
"""

Raw Strings

Raw strings (prefixed with r) do NOT support interpolation:

let name = "Alice"
let raw = r"Hello ${name}"
println(raw)  // Output: Hello ${name}  (literal, not interpolated)

Limitations

Current limitations:

  1. Simple variables only: Expressions are NOT supported inside ${}

    // This does NOT work:
    println("Sum: ${a + b}")      // ERROR
    println("Method: ${obj.get()}")  // ERROR
    
    // Use a temporary variable instead:
    let sum = a + b
    println("Sum: ${sum}")        // OK
    
  2. No format specifiers: Cannot control formatting (width, precision, etc.)

  3. No nested interpolation


Implementation Notes

String interpolation is processed at compile time by the lexer. The interpolated string is converted into a series of string concatenations in the generated C code.

Example transformation:

// Reef code
let msg = "Hello, ${name}!"

// Conceptually becomes
let msg = concat("Hello, ", concat(name, "!"))

Best Practices

  1. Use for readability: Interpolation is cleaner than multiple print() calls
  2. Pre-compute expressions: Store complex expressions in variables first
  3. Use raw strings for patterns: When $ is common (regex, shell commands)

Examples

Logging

proc log(level: string, message: string)
    println("[${level}] ${message}")
end log

proc main()
    log("INFO", "Application started")
    log("ERROR", "Something went wrong")
end main

Output:

[INFO] Application started
[ERROR] Something went wrong

Building Messages

fn format_user(name: string, id: int): string
    return "User ${name} (ID: ${id})"
end format_user

proc main()
    let msg = format_user("Alice", 42)
    println(msg)  // User Alice (ID: 42)
end main

Next: 030_TYPES.md Previous: 020_FUNCTIONS.md