Skip to content

Commit

Permalink
Merge pull request #3148 from Flowdalic/raw-string-interpolation
Browse files Browse the repository at this point in the history
string-interpolation: the raw-interpolator also performs variable substitution
  • Loading branch information
sjrd authored Jan 23, 2025
2 parents f718767 + 0c57efc commit f4d371e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions _overviews/scala3-book/string-interpolation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
```
Expand Down

0 comments on commit f4d371e

Please sign in to comment.