0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Scala 3 given/using

Modern syntax.

Modern implicits in Scala 3

Scala 3 replaces the overloaded implicit keyword with clearer syntax: given defines an implicit value, and using declares an implicit parameter. The intent is far easier to read.

object Main:
  given greeting: String = "Hello"
  def greet(name: String)(using g: String): String = s"$g, $name!"
  def main(args: Array[String]): Unit =
    println(greet("Ann"))

given replaces implicit val

Where Scala 2 wrote implicit val x: T = ..., Scala 3 writes given x: T = .... It declares a value available for implicit resolution.

object Main:
  given factor: Int = 10
  def scale(x: Int)(using f: Int): Int = x * f
  def main(args: Array[String]): Unit =
    println(scale(5))

All lessons in this course

  1. Implicit Parameters
  2. Scala 3 given/using
  3. Implicit Conversions
  4. Extension Methods
← Back to Scala for Backend Engineering & Functional Programming