Skip to content

Commit 4ba8c65

Browse files
rockpunkbishabosha
authored andcommitted
Update w/ pr comments
1 parent 4ca88cc commit 4ba8c65

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

_overviews/scala3-book/string-interpolation.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: String Interpolation
33
type: chapter
44
description: This page provides more information about creating strings and using string interpolation.
5-
languages: [es, ja, zh-cn]
5+
languages: []
66
num: 18
77
previous-page: first-look-at-types
88
next-page: control-structures
@@ -12,7 +12,7 @@ redirect_from:
1212

1313
## Introduction
1414

15-
String interpolation provides a very readable way to use variables inside strings.
15+
String interpolation provides a way to use variables inside strings.
1616
For instance:
1717

1818
{% tabs example-1 %}
@@ -25,7 +25,7 @@ println(s"$name is $age years old") // "James is 30 years old"
2525
{% endtab %}
2626
{% endtabs %}
2727

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
2929
quotes, and prefixing any variable names with a `$` symbol.
3030

3131
### Other interpolators
@@ -35,8 +35,8 @@ provides.
3535

3636
Scala provides three string interpolation methods out of the box: `s`, `f` and `raw`.
3737
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.
4040

4141
## The `s` Interpolator (`s`-Strings)
4242

@@ -222,7 +222,7 @@ In addition to the three default string interpolators, users can define their ow
222222

223223
## Advanced Usage
224224

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.
226226
This means that the compiler does some additional work to this literal. The specifics
227227
of processed strings and string interpolation are described in [SIP-11][sip-11], but
228228
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
248248
{% tabs custom-interpolator-1 %}
249249
{% tab 'Scala 2 and 3' for=custom-interpolator-1 %}
250250
```scala
251-
case class Point(x:Double, y:Double)
251+
case class Point(x: Double, y: Double)
252252

253253
val pt = p"1,-2" // Point(1.0,-2.0)
254254
```
@@ -281,26 +281,28 @@ extension (sc: StringContext)
281281
{% endtabs %}
282282

283283
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:
286288

287289
{% tabs extension-desugaring class=tabs-scala-version %}
288290

289291
{% tab 'Scala 2' for=extension-desugaring %}
290292
```scala
291-
new StringContext("1, ", "").p(var)
293+
new StringContext("1, ", "").p(someVar)
292294
```
293295

294296
The implicit class is then used to rewrite it to the following:
295297

296298
```scala
297-
new PointHelper(new StringContext("1, ", "")).p(var)
299+
new PointHelper(new StringContext("1, ", "")).p(someVar)
298300
```
299301
{% endtab %}
300302

301303
{% tab 'Scala 3' for=extension-desugaring %}
302304
```scala
303-
StringContext("1, ","").p(var)
305+
StringContext("1, ","").p(someVar)
304306
```
305307
{% endtab %}
306308

@@ -319,7 +321,7 @@ processing of the string `parts` and expression `args` instead of reusing the
319321

320322
{% tab 'Scala 2' for=naive-implementation %}
321323
```scala
322-
implicit class PointHelper(val sc:StringContext) extends AnyVal {
324+
implicit class PointHelper(val sc: StringContext) extends AnyVal {
323325
def p(args: Double*): Point = {
324326
// reuse the `s`-interpolator and then split on ','
325327
val pts = sc.s(args: _*).split(",", 2).map { _.toDoubleOption.getOrElse(0.0) }

0 commit comments

Comments
 (0)