2
2
title : String Interpolation
3
3
type : chapter
4
4
description : This page provides more information about creating strings and using string interpolation.
5
- languages : [es, ja, zh-cn ]
5
+ languages : []
6
6
num : 18
7
7
previous-page : first-look-at-types
8
8
next-page : control-structures
@@ -12,7 +12,7 @@ redirect_from:
12
12
13
13
## Introduction
14
14
15
- String interpolation provides a very readable way to use variables inside strings.
15
+ String interpolation provides a way to use variables inside strings.
16
16
For instance:
17
17
18
18
{% tabs example-1 %}
@@ -25,7 +25,7 @@ println(s"$name is $age years old") // "James is 30 years old"
25
25
{% endtab %}
26
26
{% endtabs %}
27
27
28
- Using string interpolation is as simple as putting an ` s ` in front of your string
28
+ Using string interpolation consists of putting an ` s ` in front of your string
29
29
quotes, and prefixing any variable names with a ` $ ` symbol.
30
30
31
31
### Other interpolators
@@ -35,8 +35,8 @@ provides.
35
35
36
36
Scala provides three string interpolation methods out of the box: ` s ` , ` f ` and ` raw ` .
37
37
Further, a string interpolator is a just special method so it is possible to define your
38
- own. For instance, some database libraries define the very powerful ` sql ` interpolator
39
- which returns the result of a database query.
38
+ own. For instance, some database libraries define a ` sql ` interpolator that returns a
39
+ database query.
40
40
41
41
## The ` s ` Interpolator (` s ` -Strings)
42
42
@@ -222,7 +222,7 @@ In addition to the three default string interpolators, users can define their ow
222
222
223
223
## Advanced Usage
224
224
225
- The literal ` s"Hi $name" ` is parsed by scala as a _ processed_ string literal.
225
+ The literal ` s"Hi $name" ` is parsed by Scala as a _ processed_ string literal.
226
226
This means that the compiler does some additional work to this literal. The specifics
227
227
of processed strings and string interpolation are described in [ SIP-11] [ sip-11 ] , but
228
228
here's a quick example to help illustrate how they work.
@@ -248,7 +248,7 @@ As a trivial example, let's assume we have a simple `Point` class and want to cr
248
248
{% tabs custom-interpolator-1 %}
249
249
{% tab 'Scala 2 and 3' for=custom-interpolator-1 %}
250
250
``` scala
251
- case class Point (x: Double , y: Double )
251
+ case class Point (x : Double , y : Double )
252
252
253
253
val pt = p " 1,-2 " // Point(1.0,-2.0)
254
254
```
@@ -281,26 +281,28 @@ extension (sc: StringContext)
281
281
{% endtabs %}
282
282
283
283
Once this extension is in scope and the Scala compiler encounters ` p"some string" ` , it
284
- will process ` some $string ` to turn it into String tokens and expression arguments.
285
- For example, ` p"1, $var" ` would turn into:
284
+ will process ` some string ` to turn it into String tokens and expression arguments for
285
+ each embedded variable in the string.
286
+
287
+ For example, ` p"1, $someVar" ` would turn into:
286
288
287
289
{% tabs extension-desugaring class=tabs-scala-version %}
288
290
289
291
{% tab 'Scala 2' for=extension-desugaring %}
290
292
``` scala
291
- new StringContext (" 1, " , " " ).p(var )
293
+ new StringContext (" 1, " , " " ).p(someVar )
292
294
```
293
295
294
296
The implicit class is then used to rewrite it to the following:
295
297
296
298
``` scala
297
- new PointHelper (new StringContext (" 1, " , " " )).p(var )
299
+ new PointHelper (new StringContext (" 1, " , " " )).p(someVar )
298
300
```
299
301
{% endtab %}
300
302
301
303
{% tab 'Scala 3' for=extension-desugaring %}
302
304
``` scala
303
- StringContext (" 1, " ," " ).p(var )
305
+ StringContext (" 1, " ," " ).p(someVar )
304
306
```
305
307
{% endtab %}
306
308
@@ -319,7 +321,7 @@ processing of the string `parts` and expression `args` instead of reusing the
319
321
320
322
{% tab 'Scala 2' for=naive-implementation %}
321
323
``` scala
322
- implicit class PointHelper (val sc : StringContext ) extends AnyVal {
324
+ implicit class PointHelper (val sc : StringContext ) extends AnyVal {
323
325
def p (args : Double * ): Point = {
324
326
// reuse the `s`-interpolator and then split on ','
325
327
val pts = sc.s(args : _* ).split(" ," , 2 ).map { _.toDoubleOption.getOrElse(0.0 ) }
0 commit comments