Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Implicit

Scala's implicits are a powerful feature that allows the compiler to "fill in" values or methods automatically, enhancing the language's expressiveness and enabling more concise code. They are especially useful for type class patterns, extension methods, and implicit parameters, among others. Here, we'll explore an introductory tutorial covering several key aspects of Scala implicits.

Implicit Values

Implicit values are automatically used by the compiler to fill in parameters for methods or constructors.

implicit val defaultName: String = "John Doe"

def greet(implicit name: String): Unit = {
  println(s"Hello, $name!")
}

greet  // Output: Hello, John Doe!

Implicit Conversions

Implicit conversions can automatically convert one type to another, allowing for cleaner code when working with different but related types.

implicit def intToString(value: Int): String = value.toString

val myString: String = 123  // Implicitly converts Int to String
println(myString)  // Output: 123

Implicit Classes

Implicit classes allow adding new methods to existing types without modifying their source code, similar to extension methods in other languages.

implicit class RichInt(val i: Int) {
  def squared: Int = i * i
}

println(5.squared)  // Output: 25

Implicit Parameters

Implicit parameters enable passing context or configuration to methods without cluttering their call sites.

case class Config(prefix: String)

implicit val myConfig: Config = Config("Hello, ")

def greet(name: String)(implicit config: Config): Unit = {
  println(config.prefix + name)
}

greet("Scala")  // Output: Hello, Scala

Best Practices

While powerful, implicits should be used judiciously:

  • Limit Scope: Only bring implicits into scope when necessary to avoid conflicts and confusion.
  • Naming Conventions: Use clear and descriptive names for implicit values and parameters.
  • Documentation: Document your implicits well to aid in understanding and maintenance.