Back to Scala3

E016: Interpolated String Error

docs/_docs/reference/error-codes/E016.md

3.8.41.8 KB
Original Source

E016: Interpolated String Error

This error is emitted when an interpolated string contains invalid syntax after a $ sign. An identifier or a block expression ${...} is expected.

In string interpolation, the $ character is used to embed expressions in strings. After $, you can use either:

  • A simple identifier: $name
  • A block expression: ${expression}

If you need to include a complex expression (like new Object()), you must wrap it in braces.


Example

scala
val x = s"$new Object()"

Error

scala
-- [E016] Syntax Error: example.scala:1:11 -------------------------------------
1 |val x = s"$new Object()"
  |           ^
  |           Error in interpolated string: identifier or block expected
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | This usually happens when you forget to place your expressions inside curly braces.
  |
  | s"$new Point(0, 0)"
  |
  | should be written as
  |
  | s"${new Point(0, 0)}"
   -----------------------------------------------------------------------------

Solution

scala
// Wrap complex expressions in braces
val x = s"${new Object()}"
scala
// Simple identifiers don't need braces
val name = "World"
val greeting = s"Hello, $name!"
scala
// Use braces for member access
case class Person(name: String)
val person = Person("Alice")
val greeting = s"Hello, ${person.name}!"
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>