Scala 3 given/using
Modern syntax.
Scala 3 given/using is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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))using replaces implicit parameters
A using clause marks a parameter list as implicit. The compiler fills it from a matching given in scope.
object Main:
given unit: String = "kg"
def label(n: Int)(using u: String): String = s"$n$u"
def main(args: Array[String]): Unit =
println(label(75))Anonymous givens
You can omit the name of a given when you never refer to it directly. The compiler still finds it by type.
object Main:
given Int = 3
def triple(x: Int)(using f: Int): Int = x * f
def main(args: Array[String]): Unit =
println(triple(7))Summoning with summon
Scala 3's summon[T] replaces implicitly[T]. It fetches the given value of type T directly.
object Main:
given name: String = "Scala 3"
def main(args: Array[String]): Unit =
val n = summon[String]
println(n)Anonymous using parameters
If you only need to forward a using parameter, you can leave it unnamed in the clause: (using Int). Summon it inside with summon.
object Main:
given Int = 100
def boosted(x: Int)(using Int): Int = x + summon[Int]
def main(args: Array[String]): Unit =
println(boosted(5))given for type classes
The most idiomatic use of given/using is type classes. Define a trait, provide a given instance, and require it with using.
trait Show[A]:
def show(a: A): String
object Main:
given Show[Int] with
def show(a: Int): String = s"Int($a)"
def display[A](a: A)(using s: Show[A]): String = s.show(a)
def main(args: Array[String]): Unit =
println(display(42))Context bounds still work
Context bounds [A: Show] remain available and desugar to a using parameter. Combine with summon to access the instance.
trait Show[A]:
def show(a: A): String
object Main:
given Show[String] with
def show(a: String): String = s"Str($a)"
def display[A: Show](a: A): String = summon[Show[A]].show(a)
def main(args: Array[String]): Unit =
println(display("hi"))Passing using arguments explicitly
You can supply a using parameter explicitly with the using keyword at the call site, overriding the given in scope.
object Main:
given Int = 10
def scale(x: Int)(using f: Int): Int = x * f
def main(args: Array[String]): Unit =
println(scale(5))
println(scale(5)(using 2))given imports
Scala 3 has dedicated syntax to import givens: import pkg.given or import pkg.{given Show[Int]}. This makes implicit imports explicit and intentional.
object Instances:
given Int = 5
object Main:
import Instances.given
def addBase(x: Int)(using b: Int): Int = x + b
def main(args: Array[String]): Unit =
println(addBase(10))Why the new syntax
In Scala 2 the single keyword implicit meant several different things. Scala 3 splits the concept: given = provide, using = require. Code is clearer and errors are easier to understand.
object Main:
given pi: Double = 3.14159
def area(r: Double)(using p: Double): Double = p * r * r
def main(args: Array[String]): Unit =
println(area(2.0))Quick Check
In Scala 3, which keywords replace implicit for providing and requiring values?
Recap
You learned Scala 3's given/using:
givenprovides an implicit value (replacesimplicit val)usingdeclares an implicit parametersummon[T]replacesimplicitly[T]- Givens can be anonymous and ideal for type classes
import pkg.givenimports them explicitly
Frequently asked questions
Is the “Scala 3 given/using” lesson free?
Yes — the full text of “Scala 3 given/using” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “Scala 3 given/using”?
Modern syntax. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Scala 3 given/using” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Implicit Parameters
- Scala 3 given/using
- Implicit Conversions
- Extension Methods