docs/_docs/reference/error-codes/E209.md
This error occurs when there is a type mismatch or invalid format specifier in an f"" string interpolator. The f interpolator uses Java's Formatter syntax and requires that the format specifiers match the types of the interpolated values.
def example =
val s = "hello"
f"$s%d"
-- [E209] Interpolation Error: example.scala:3:5 -------------------------------
3 | f"$s%d"
| ^
| Found: (s : String), Required: Int, Long, Byte, Short, BigInt
Use the correct format specifier for the value's type. For strings, use %s:
def example =
val s = "hello"
f"$s%s"
Here are some common format specifiers and their expected types:
| Specifier | Expected Type | Example |
|---|---|---|
%s | Any (converted to String) | f"$name%s" |
%d | Int, Long, Byte, Short, BigInt | f"$count%d" |
%f | Double, Float, BigDecimal | f"$price%f" |
%x | Int, Long, Byte, Short, BigInt | f"$hex%x" |
%c | Char, Byte, Short, Int | f"$char%c" |
%b | Boolean (or any for null check) | f"$flag%b" |
%e | Double, Float, BigDecimal | f"$scientific%e" |
def formatNumbers =
val price = 19.99
val count = 42
f"Price: $$$price%.2f, Count: $count%04d"