O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Implicit Conversions

Let’s create a practical example of implicit conversions using string interpolators. Scala has some nice built-in string interpolators—see String Interpolation—but you can also create your own custom interpolators quite easily by using the concepts we’ve seen in this chapter. We’ll create a custom interpolator that will mask out select values from the processed string. To show that the interpolation can return something other than String—just about any object—we’ll return a StringBuilder as the result of processing the string. But first let’s revisit string interpolation to explore some concepts we need for the implementation.

When Scala sees the form

 
interpolatorName"text1 $expr1 text2 $expr2"

the compiler turns ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required