This class provides the basic mechanism to do String Interpolation. String Interpolation allows users to embed variable references directly in *processed* string literals. Here's an example:
val name = "James"
println(s"Hello, $name") // Hello, James
Any processed string literal is rewritten as an instantiation and method call against this class. For example:
s"Hello, $name"
is rewritten to be:
StringContext("Hello, ", "").s(name)
By default, this class provides the raw
, s
and f
methods as available interpolators.
To provide your own string interpolator, create an implicit class which adds a method to StringContext
. Here's an example:
implicit class JsonHelper(private val sc: StringContext) extends AnyVal {
def json(args: Any*): JSONObject = ...
}
val x: JSONObject = json"{ a: $a }"
Here the JsonHelper
extension class implicitly adds the json
method to StringContext
which can be used for json
string literals.
Value parameters
- parts
-
The parts that make up the interpolated string, without the expressions that get inserted by interpolation.
Attributes
- Companion
- object
- Source
- StringContext.scala
- Graph
-
- Supertypes
Members list
- no keywords
- erased
- Not inherited
- Product