Skip to content

Usage of Implicit in Scala!

At this moment I do not have a personal relationship with a computer.

Janet Reno

In Scala, implicits are a powerful feature that allows for some syntactic sugar, as well as providing a way to provide implicit conversions, parameters, and values.

Here are the main ways implicits are used in Scala:

  1. Implicit parameters: Implicit parameters are function arguments that are automatically passed by the compiler. They are defined using the implicit keyword in the function definition, and can be used when the function is called if there is no corresponding argument provided explicitly. For example:
def greet(name: String)(implicit greeting: String): Unit = {
  println(s"$greeting, $name!")
}

implicit val hello: String = "Hello"

greet("Alice") // Output: Hello, Alice!

In this example, the greet function takes an implicit parameter greeting, which is used to customize the greeting. When we call the greet function with just the name argument, the compiler looks for an implicit value of type String that matches the parameter name greeting, and finds the hello value that we defined implicitly.

  1. Implicit conversions: Implicit conversions allow the compiler to automatically convert a value from one type to another, based on some predefined conversion rules. They are defined using an implicit function, which takes the original value as an argument and returns the converted value. For example:
implicit def intToString(i: Int): String = i.toString

val str: String = 42
println(str) // Output: 42

In this example, we define an implicit conversion function intToString that converts an Int to a String. When we assign the integer 42 to a variable of type String, the compiler automatically applies the implicit conversion to convert it to a string.

  1. Implicit classes: Implicit classes provide a way to extend existing classes with new methods, without modifying the original class. They are defined using the implicit keyword before the class definition, and take a single parameter that is the type being extended. For example:
implicit class StringOps(s: String) {
  def repeat(n: Int): String = {
    List.fill(n)(s).mkString("")
  }
}

val s = "hello"
println(s.repeat(3)) // Output: "hellohellohello"

In this example, we define an implicit class StringOps that adds a repeat method to the String class. When we call the repeat method on a string, the compiler automatically applies the implicit conversion to convert the string to an instance of the StringOps class.

Implicits are a powerful feature in Scala, but they can also be abused, leading to code that is difficult to read and understand. It’s important to use implicits sparingly and judiciously, and to make sure that the code remains clear and maintainable.

Published inPersonal PostsScalaTechnical Posts