From 0c57efc06a93e0dfd88497d62cb1aa24e5ea39dc Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 22 Jan 2025 13:13:18 +0100 Subject: [PATCH] string-interpolation: the raw-interpolator also performs variable substitution Clarify that the raw-interpolator also performs variable substitution. This was not obvious from the text and it is easy to believe that raw means just that: the raw string, as is, without any substitutions performed. This also moves the last sentence of the raw-interpolator section into the following section, where it logicaly belongs. --- _overviews/scala3-book/string-interpolation.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/_overviews/scala3-book/string-interpolation.md b/_overviews/scala3-book/string-interpolation.md index e1c4f10054..0bf4e1e0b8 100644 --- a/_overviews/scala3-book/string-interpolation.md +++ b/_overviews/scala3-book/string-interpolation.md @@ -211,10 +211,22 @@ res1: String = a\nb The raw interpolator is useful when you want to avoid having expressions like `\n` turn into a return character. -In addition to the three default string interpolators, users can define their own. +Furthermore, the raw interpolator allows the usage of variables, which are replaced with their value, just as the s interpolator. + +{% tabs example-11 %} +{% tab 'Scala 2 and 3' for=example-11 %} +```scala +scala> val foo = 42 +scala> raw"a\n$foo" +res1: String = a\n42 +``` +{% endtab %} +{% endtabs %} ## Advanced Usage +In addition to the three default string interpolators, users can define their own. + The literal `s"Hi $name"` is parsed by Scala as a _processed_ string literal. This means that the compiler does some additional work to this literal. The specifics of processed strings and string interpolation are described in [SIP-11][sip-11], but @@ -224,8 +236,8 @@ here's a quick example to help illustrate how they work. In Scala, all processed string literals are simple code transformations. Anytime the compiler encounters a processed string literal of the form: -{% tabs example-11 %} -{% tab 'Scala 2 and 3' for=example-11 %} +{% tabs example-12 %} +{% tab 'Scala 2 and 3' for=example-12 %} ```scala id"string content" ```