MOCKSTACKS
EN
Questions And Answers

More Tutorials









Scala String Interpolation

Examples

Hello String Interpolation


The s interpolator allows the usage of variables within a string.

val name = "Brian"
println(s"Hello $name")

prints "Hello Brian" to the console when ran.

Formatted String Interpolation Using the f Interpolator


val num = 42d

Print two decimal places for num using f

println(f"$num%2.2f")
42.00

Print num using scientific notation using e

println(f"$num%e")
4.200000e+01

Print num in hexadecimal with a

println(f"$num%a")
0x1.5p5

Using expression in string literals


You can use curly braces to interpolate expressions into string literals:

def f(x: String) = x + x
val a = "A"
s"${a}" // "A"
s"${f(a)}" // "AA"

Without the braces, scala would only interpolate the identifier after the $ (in this case f). Since there is no implicit conversion from f to a String this is an exception in this example:

s"$f(a)" // compile-time error (missing argument list for method f)

Custom string interpolators


It is possible to define custom string interpolators in addition to the built-in ones.

my"foo${bar}baz"

Is expanded by the compiler to:

new scala.StringContext("foo", "baz").my(bar)

scala.StringContext has no my method, therefore it can be provided by implicit conversion. A custom interpolator with the same behavior as the builtin s interpolator would then be implemented as follows:

implicit class MyInterpolator(sc: StringContext) {
 def my(subs: Any*): String = {
 val pit = sc.parts.iterator
 val sit = subs.iterator
 // Note parts.length == subs.length + 1
 val sb = new java.lang.StringBuilder(pit.next())
 while(sit.hasNext) {
 sb.append(sit.next().toString)
 sb.append(pit.next())
 }
 sb.toString
 }
}

And the interpolation my"foo${bar}baz" would desugar to:

new MyInterpolation(new StringContext("foo", "baz")).my(bar)

Note that there is no restriction on the arguments or return type of the interpolation function. This leads us down a dark path where interpolation syntax can be used creatively to construct arbitrary objects, as illustrated in the following example:

case class Let(name: Char, value: Int)
implicit class LetInterpolator(sc: StringContext) {
 def let(value: Int): Let = Let(sc.parts(0).charAt(0), value)
}
let"a=${4}" // Let(a, 4)
let"b=${"foo"}" // error: type mismatch
let"c=" // error: not enough arguments for method let: (value: Int)Let


Conclusion

In this page (written and validated by ) you learned about Scala String Interpolation . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Symbol Literals.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.