8000 Merge pull request #563 from marlomajor/master · dk14/scala.github.com@aa766ed · GitHub
[go: up one dir, main page]

Skip to content

Commit aa766ed

Browse files
authored
Merge pull request scala#563 from marlomajor/master
change precedding
2 parents 80a7503 + 3ffaea0 commit aa766ed

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

overviews/core/_posts/2012-09-21-string-interpolation.md

< 8000 button data-component="IconButton" type="button" class="prc-Button-ButtonBase-c50BI ml-1 flex-shrink-0 prc-Button-IconButton-szpyj" data-loading="false" data-no-visuals="true" data-size="medium" data-variant="invisible" aria-describedby=":R3krtlab:-loading-announcement" aria-labelledby=":R4rtlab:">
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ String Interpolation allows users to embed variable references directly in *proc
1919
println(s"Hello, $name") // Hello, James
2020

2121
In the above, the literal `s"Hello, $name"` is a *processed* string literal. This means that the compiler does some additional
22-
work to this literal. A processed string literal is denoted by a set of characters precedding the `"`. String interpolation
22+
work to this literal. A processed string literal is denoted by a set of characters preceding the `"`. String interpolation
2323
was introduced by [SIP-11](http://docs.scala-lang.org/sips/pending/string-interpolation.html), which contains all details of the implementation.
2424

2525
## Usage
@@ -56,15 +56,15 @@ The `f` interpolator is typesafe. If you try to pass a format string that only
5656
error. For example:
5757

5858
val height: Double = 1.9d
59-
59+
6060
scala> f"$height%4d"
6161
<console>:9: error: type mismatch;
6262
found : Double
6363
required: Int
6464
f"$height%4d"
6565
^
6666

67-
The `f` interpolator makes use of the string format utilities available from Java. The formats allowed after the `%` character are outlined in the
67+
The `f` interpolator makes use of the string format utilities available from Java. The formats allowed after the `%` character are outlined in the
6868
[Formatter javadoc](http://docs.oracle.com/javase/1.6.0/docs/api/java/util/Formatter.html#detail). If there is no `%` character after a variable
6969
definition a formatter of `%s` (`String`) is assumed.
7070

@@ -74,7 +74,7 @@ definition a formatter of `%s` (`String`) is assumed.
7474
The raw interpolator is similar to the `s` interpolator except that it performs no escaping of literals within the string. Here's an example processed string:
7575

7676
scala> s"a\nb"
77-
res0: String =
77+
res0: String =
7878
a
7979
b
8080

@@ -98,14 +98,14 @@ it transforms it into a method call (`id`) on an instance of [StringContext](htt
9898
This method can also be available on implicit scope. To define our own string interpolation, we simply need to create an implicit class that adds a new method
9999
to `StringContext`. Here's an example:
100100

101-
// Note: We extends AnyVal to prevent runtime instantiation. See
101+
// Note: We extends AnyVal to prevent runtime instantiation. See
102102
// value class guide for more info.
103103
implicit class JsonHelper(val sc: StringContext) extends AnyVal {
104104
def json(args: Any*): JSONObject = sys.error("TODO - IMPLEMENT")
105105
}
106-
106+
107107
def giveMeSomeJson(x: JSONObject): Unit = ...
108-
108+
109109
giveMeSomeJson(json"{ name: $name, id: $id }")
110110

111111
In this example, we're attempting to create a JSON literal syntax using string interpolation. The JsonHelper implicit class must be in scope to use this syntax, and the json method would need a complete implementation. However, the result of such a formatted string literal would not be a string, but a `JSONObject`.
@@ -134,4 +134,3 @@ So, the `json` method has access to the raw pieces of strings and each expressio
134134
}
135135

136136
Each of the string portions of the processed string are exposed in the `StringContext`'s `parts` member. Each of the expression values is passed into the `json` method's `args` parameter. The `json` method takes this and generates a big string which it then parses into JSON. A more sophisticated implementation could avoid having to generate this string and simply construct the JSON directly from the raw strings and expression values.
137-

0 commit comments

Comments
 (0)
0