Scala 3: From Implicits to Givens

Scala 3 replaces implicit with given/using — a clearer model for contextual abstractions.

Key Insights

  • given replaces implicit val/def for providing type class instances
  • using replaces implicit parameters with clearer intent
  • Extension methods replace implicit classes for adding methods to types

Given Instances

trait Ordering[T]:
  def compare(a: T, b: T): Int

given Ordering[Int] with
  def compare(a: Int, b: Int): Int = a - b

Using Clauses

def sorted[T](list: List[T])(using ord: Ordering[T]): List[T] =
  list.sortWith((a, b) => ord.compare(a, b) < 0)

// Compiler finds the given instance automatically
val result = sorted(List(3, 1, 2))

Extension Methods

extension (s: String)
  def words: List[String] = s.split("\\s+").toList
  def initials: String = words.map(_.head.toUpper).mkString

"hello world".initials  // "HW"

Liked this? There's more.

Every week: one practical technique, explained simply, with code you can use immediately.