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
val x = s"$new Object()"
Error
-- [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
// Wrap complex expressions in braces
val x = s"${new Object()}"
// Simple identifiers don't need braces
val name = "World"
val greeting = s"Hello, $name!"
// Use braces for member access
case class Person(name: String)
val person = Person("Alice")
val greeting = s"Hello, ${person.name}!"
In this article