Skip to content

string-interpolation: the raw-interpolator also performs variable substitution #3148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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