100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Implicits in Scala

Scala's implicit parameters, implicit conversions, and implicit classes let the compiler thread context and behavior through code automatically, powering type classes and DSLs.

Advanced ScalaAdvanced10 min readJul 10, 2026
Analogies

What Are Implicits?

Scala's implicit mechanism lets the compiler automatically supply a value, a conversion, or a method that the programmer did not write explicitly at the call site. There are three related but distinct forms: implicit parameters (the compiler fills in a missing argument by searching implicit scope), implicit conversions (the compiler inserts a call to an implicit def or implicit class to make an otherwise-mismatched type compile), and implicit classes (which add extension methods to existing types without subclassing or modifying the original source). All three are resolved entirely at compile time using static type information, so there is zero runtime reflection cost, but the trade-off is that code can silently do more than what is written, which is why teams adopt conventions to keep implicit usage predictable.

🏏

Cricket analogy: Like a substitute fielder the umpire sends onto the ground without the captain naming him individually - the twelfth man fills a specific role automatically based on rules already agreed, not a fresh decision each time, just as implicit resolution fills a parameter.

Implicit Parameters

An implicit parameter list is declared with the implicit keyword on the last parameter group of a method, and the compiler fills it in by searching two places: the local implicit scope (values marked implicit that are in lexical scope, including imports) and the implicit scope of the parameter's type (its companion object). This is exactly how List(3,1,2).sorted works without you passing a comparator - sorted takes an implicit Ordering[A], and Ordering.Int lives in Ordering's companion object, so it is found automatically. You can override the default by bringing your own implicit val into scope, which shadows the companion-object default because local scope wins.

🏏

Cricket analogy: Like how sorted finds Ordering.Int in a companion object the way a bowling coach automatically assigns a death-over specialist such as Jasprit Bumrah when no captain specifies who bowls the 19th over - the default resolves from established team roles.

scala
// sorted resolves an implicit Ordering[Int] from Ordering's companion object
val nums = List(3, 1, 2)
println(nums.sorted)              // List(1, 2, 3)

// A local implicit shadows the companion-object default
implicit val descending: Ordering[Int] = Ordering.Int.reverse
println(nums.sorted)              // List(3, 2, 1)

Implicit Conversions

An implicit conversion is a method or implicit class marked implicit that the compiler inserts automatically when it finds a type mismatch it can resolve - for example, converting an Int to a RichInt to call .until on it. Scala 2 requires explicitly importing scala.language.implicitConversions (or the -language:implicitConversions flag) to use bare implicit def conversions, precisely because unrestrained implicit conversions make code hard to trace: a method call can appear to work on a type that never defined it, and the actual applicable conversion may be buried in an unrelated import. Scala 3 replaces this mechanism with Conversion[A, B] given instances, which are more discoverable because they carry an explicit marker type rather than being bare defs.

🏏

Cricket analogy: Like a DRS review silently reclassifying a marginal caught-behind decision from 'not out' to 'out' based on Snicko data the umpire didn't announce verbally - the conversion happens, but a viewer unfamiliar with the process might not trace why the decision changed.

Implicit conversions are the most misused of the three implicit forms - because they run invisibly at any type-mismatch point, a stray import can silently change a program's meaning. Scala 2 gates them behind import scala.language.implicitConversions, and Scala 3 replaces raw implicit def conversions with typed given Conversion[A, B] instances that are easier for tooling to flag and for reviewers to spot in a diff. Prefer explicit .toX methods or extension methods over broad implicit conversions whenever you can.

Implicit Classes and Type Classes

An implicit class wraps a value in a decorator without modifying the original type's source, enabling the 'pimp my library' pattern - for instance, adding an .isPalindrome method to String. Combined with implicit parameters, this becomes the type class pattern: you define a trait like JsonWriter[A] describing a capability, provide implicit instances for the types that support it (JsonWriter[Int], JsonWriter[Person]), and write generic code with an implicit parameter (implicit w: JsonWriter[A]) that the compiler resolves per call site. This gives you ad-hoc polymorphism - the ability to add behavior to types you don't own, including types from third-party libraries - without inheritance or modifying those types.

🏏

Cricket analogy: Like a broadcaster adding a 'pressure index' overlay stat to a player's profile without changing the player's actual official match record - an implicit class decorates the existing type with new capability instead of editing the source.

Implicit resolution searches, in order: (1) explicitly imported or locally declared implicits in the current scope, and (2) the implicit scope of the involved types - their companion objects and the companion objects of their type parameters. Scala 3 renames the mechanism to given (declaring) and using (requesting), and requires given instances to be looked up by type rather than by an arbitrary name, which makes ambiguous-implicit errors easier to diagnose.

scala
trait JsonWriter[A] {
  def write(value: A): String
}

object JsonWriter {
  implicit val intWriter: JsonWriter[Int] = (value: Int) => value.toString
  implicit val stringWriter: JsonWriter[String] = (value: String) => s"\"$value\""

  implicit def listWriter[A](implicit itemWriter: JsonWriter[A]): JsonWriter[List[A]] =
    (values: List[A]) => values.map(itemWriter.write).mkString("[", ",", "]")
}

implicit class JsonSyntax[A](value: A) {
  def toJson(implicit writer: JsonWriter[A]): String = writer.write(value)
}

import JsonWriter._
println(List(1, 2, 3).toJson)   // [1,2,3]
println("hello".toJson)         // "hello"
  • Implicit parameters let the compiler fill in a missing argument by searching local scope and then the companion objects of the parameter's type.
  • Implicit conversions insert a type-fixing method call automatically and must be explicitly enabled via import scala.language.implicitConversions in Scala 2.
  • Implicit classes add extension methods to existing types without modifying their source, enabling the 'pimp my library' pattern.
  • Combining implicit classes with implicit parameters produces the type class pattern, Scala's mechanism for ad-hoc polymorphism.
  • Local implicits always shadow companion-object implicits, letting call sites override defaults deliberately.
  • Scala 3 replaces implicit with the more explicit given/using keywords and typed Conversion[A, B] instances to reduce ambiguity.
  • Overused implicit conversions are the main source of 'magic' Scala code; prefer explicit methods where clarity matters more than brevity.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#ScalaStudyNotes#ImplicitsInScala#Implicits#Scala#Implicit#Parameters#StudyNotes#SkillVeris#ExamPrep