You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<
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:">Copy file name to clipboardExpand all lines: overviews/core/_posts/2012-09-21-string-interpolation.md
+7-8Lines changed: 7 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ String Interpolation allows users to embed variable references directly in *proc
19
19
println(s"Hello, $name") // Hello, James
20
20
21
21
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
23
23
was introduced by [SIP-11](http://docs.scala-lang.org/sips/pending/string-interpolation.html), which contains all details of the implementation.
24
24
25
25
## Usage
@@ -56,15 +56,15 @@ The `f` interpolator is typesafe. If you try to pass a format string that only
56
56
error. For example:
57
57
58
58
val height: Double = 1.9d
59
-
59
+
60
60
scala> f"$height%4d"
61
61
<console>:9: error: type mismatch;
62
62
found : Double
63
63
required: Int
64
64
f"$height%4d"
65
65
^
66
66
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
68
68
[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
69
69
definition a formatter of `%s` (`String`) is assumed.
70
70
@@ -74,7 +74,7 @@ definition a formatter of `%s` (`String`) is assumed.
74
74
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:
75
75
76
76
scala> s"a\nb"
77
-
res0: String =
77
+
res0: String =
78
78
a
79
79
b
80
80
@@ -98,14 +98,14 @@ it transforms it into a method call (`id`) on an instance of [StringContext](htt
98
98
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
99
99
to `StringContext`. Here's an example:
100
100
101
-
// Note: We extends AnyVal to prevent runtime instantiation. See
101
+
// Note: We extends AnyVal to prevent runtime instantiation. See
102
102
// value class guide for more info.
103
103
implicit class JsonHelper(val sc: StringContext) extends AnyVal {
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
134
134
}
135
135
136
136
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.
0 commit comments