Learn Scala 3 Fast: String Interpolators

Before we move on to other data types, it will be helpful to demonstrate two more things about Scala strings:

  • String interpolators
  • Multiline strings

In this lesson we’ll look at string interpolators, and in the next lesson we’ll look at multiline strings.

String interpolators

Scala has the notion of “string interpolators.” For example, given these two variables:

val firstName = "Alvin"
val lastName = "Alexander"

You can print the full name like this:

println(firstName + " " + lastName)   // prints "Alvin Alexander"

That’s how we used to do things 20 years ago in other languages, but with Scala the preferred way to print that same result is like this:

println(s"$firstName $lastName")      // prints "Alvin Alexander"

Notice that the string is prepended with the character s:

println(s"$firstName $lastName")

This is Scala’s way of letting you declare that the following string should be interpolated, meaning that the string contains variables that should be interpreted before they are put into the string.

I’ll explain the reason for the s character in just a moment, but before doing that, it’s important to note that when you use an expression inside an interpolated string, you need to use curly braces around the expression:

println(s"Two plus two equals ${2 + 2}")
println(s"Two times two equals ${2 * 2}")

So far I’ve shown this technique with println statements, but you can use it anywhere you use a String:

val x = s"Two plus two equals ${2 + 2}"

Other interpolators

Now I can explain the reason for the letter s that precedes the string: s is a method, and it provides just one way that a string can be interpolated. For instance, there’s another built-in interpolator named f that lets you format strings just like the C programming language printf syntax.

For example, I once wrote a little logging library for Scala, and in it I use the f interpolator like this:

bw.write(f"$time | $logLevel%-5s | $classname | $msg\n")

This pads the second field (logLevel) to be five characters wide and make it left-justified, so its output looks like this:

04:52:51:541 | INFO  | Bar | this is an info message from class Bar
04:52:51:541 | WARN  | Bar | this is a warn message from class Bar
04:52:51:541 | DEBUG | Bar | this is an error message from class Bar

In addition to the s and f interpolators, there are other interpolators, and a real key is that you can write your own interpolators! For example, Scala SQL libraries typically offer a sql interpolator that looks like this:

val result = sql"select * from $table where $name = $value"

In more complex examples that sql interpolator can work with multiple column names and multiple values, and its return type can be something other than a String. For instance, depending on the library, it can return a SqlStatement or something similar (i.e., some fully-formatted and ready to run SQL).

If you’re like me, when you first saw that s before the string it may have struck you as unusual, but now that you see the logic behind it, it turns out to be a terrific benefit. Library writers often take advantage of this.


The exercises for this lesson are available here.

More information

If you ever want to write your own string interpolator, see my tutorial, How to create your own Scala 3 String interpolator.

books by alvin