보간 안에서의 이스케이프

보간 안에서의 이스케이프 (Escapes in interpolations)

Scala 2에서는 따옴표 문자열 보간(quoted interpolation) 안에서 큰따옴표 문자 "를 곧바로 표현할 방법이 없었어요(삼중 따옴표 보간은 예외죠). 거기에 \ 문자를 쓰기도 어려워요. 보간자(interpolator)가 이스케이프를 어떻게 처리할지 스스로 결정하기 때문에, 파서는 "가 이스케이프되어야 하는지, 아니면 종결자로 쓰이는 건지 알 수 없거든요.

출처: Scala 3 Reference

본문

Scala 3에서는 보간의 메타 문자 $를 사용해서 " 문자를 이스케이프할 수 있어요. 예를 들어볼게요.

val inventor = "Thomas Edison"
val interpolation = s"as $inventor said: $\"The three great essentials to achieve anything worth while are: Hard work, Stick-to-itiveness, and Common sense.$\""

보간 안에서 $"처럼 쓰면, 그 "는 보간 문자열을 끝내는 종결자가 아니라 내용의 일부로 취급돼요. 그래서 따옴표로 감싼 인용문을 문자열 보간 안에 그대로 넣을 수 있죠.

더 알아보기 (Learn more)