Monday, August 13, 2018

Scala String interpolation

Introduction

Starting in Scala 2.10.0, Scala offers a new mechanism to create strings from your data: String Interpolation. String Interpolation allows users to embed variable references directly in processedstring literals. Here’s an example:
  1. val name = "James"
  2. println(s"Hello, $name") // Hello, James
In the above, the literal s"Hello, $name" is a processed string literal. This means that the compiler does some additional work to this literal. A processed string literal is denoted by a set of characters precedding the ". String interpolation was introduced by SIP-13, which contains all details of the implementation.

Usage

Scala provides three string interpolation methods out of the box: s, f and raw.

The s String Interpolator

Prepending s to any string literal allows the usage of variables directly in the string. You’ve already seen an example here:
  1. val name = "James"
  2. println(s"Hello, $name") // Hello, James
Here $name is nested inside an s processed string. The s interpolator knows to insert the value of the name variable at this location in the string, resulting in the string Hello, James. With the sinterpolator, any name that is in scope can be used within a string.
String interpolators can also take arbitrary expressions. For example:
  1. println(s"1 + 1 = ${1 + 1}")
will print the string 1 + 1 = 2. Any arbitrary expression can be embedded in ${}.

The f Interpolator

Prepending f to any string literal allows the creation of simple formatted strings, similar to printfin other languages. When using the f interpolator, all variable references should be followed by aprintf-style format string, like %d. Let’s look at an example:
  1. val height = 1.9d
  2. val name = "James"
  3. println(f"$name%s is $height%2.2f meters tall") // James is 1.90 meters tall
The f interpolator is typesafe. If you try to pass a format string that only works for integers but pass a double, the compiler will issue an error. For example:
  1. val height: Double = 1.9d
  2. scala> f"$height%4d"
  3. <console>:9: error: type mismatch;
  4. found : Double
  5. required: Int
  6. f"$height%4d"
  7. ^
The f interpolator makes use of the string format utilities available from Java. The formats allowed after the % character are outlined in the Formatter javadoc. If there is no % character after a variable definition a formatter of %s (String) is assumed.

The raw Interpolator

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:
  1. scala> s"a\nb"
  2. res0: String =
  3. a
  4. b
Here the s string interpolator replaced the characters \n with a return character. The rawinterpolator will not do that.
  1. scala> raw"a\nb"
  2. res1: String = a\nb
The raw interpolator is useful when you want to avoid having expressions like \n turn into a return character.
In addition to the three default string interpolators, users can define their own.
example:
package com.sagar.basic
import scala.math._
object Hello extends App {
println("Hello World");
//String Interpolation Example
val Tau = Pi * 2
println(s"Valie of tau is $Tau");
//String Interpolation example with s & f
println("Result for string interpolation with s:" + s"Happy $Tau Day")
println("Result for string interpolation with f:" + f"Happy $Tau%2.2f Day")
//String Formating
printf("Now you have %.7f problems.", Math.nextAfter(2.0, 3))
}

No comments:

Post a Comment

How to check whether operating system is 64 bit or 32bit?

What is 32 and 64 bit operating system? The terms 32-bit and 64-bit refer to the way a computer's processor that is CPU, handles info...