E209: Format Interpolation Error
E209: Format Interpolation Error
이 에러는 f"" 문자열 인터폴레이터에서 타입 불일치나 잘못된 형식 지정자가 있을 때 발생해요. f 인터폴레이터는 Java의 Formatter 문법을 사용하며, 형식 지정자가 인터폴레이션되는 값의 타입과 일치해야 해요.
본문
예시 (Example)
def example =
val s = "hello"
f"$s%d"
에러 (Error)
-- [E209] Interpolation Error: example.scala:3:5 -------------------------------
3 | f"$s%d"
| ^
| Found: (s : String), Required: Int, Long, Byte, Short, BigInt
해결 방법 (Solution)
값의 타입에 맞는 올바른 형식 지정자를 사용해요. 문자열에는 %s를 사용해요.
def example =
val s = "hello"
f"$s%s"
자주 쓰는 형식 지정자 (Common Format Specifiers)
다음은 자주 쓰이는 형식 지정자와 각각이 기대하는 타입이에요.
| 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" |
숫자 형식 지정 예시 (Example with Numeric Formatting)
def formatNumbers =
val price = 19.99
val count = 42
f"Price: $$$price%.2f, Count: $count%04d"