Given and Using
given and using let the compiler pass context (like a formatter or configuration) automatically, enabling type classes.
On this page · 5 min · Advanced
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Oct 30, 2026
Related tutorials
In simple words#
given is like registering your default delivery address once. Every order (using) automatically ships there — you only specify a different one when you want to.
- given
- A value the compiler can supply automatically.
- using
- A parameter the compiler fills with a matching given.
- Type class
- Behaviour defined for types without changing them (e.g. Show[A]).
Learning objectives#
- Define given instances
- Write methods with using parameters
- Build a simple type class
- Use built-in Ordering givens
Explanation#
given Ordering[Product] = Ordering.by(_.price) provides an ordering wherever one is needed.
def show[A](a: A)(using s: Show[A]) asks the compiler to find a Show[A].
Type classes add behaviour to types you don’t own (Int, String, third-party classes).
Givens are imported explicitly with import mypackage.given — this replaces Scala 2’s implicit.
Code example: A Show type class#
trait Show[A]:
def show(a: A): String
case class Product(name: String, price: Double)
given Show[Int] with
def show(n: Int) = s"#$n"
given Show[Product] with
def show(p: Product) = f"${p.name} (₹${p.price}%,.0f)"
def printAll[A](items: List[A])(using s: Show[A]): Unit =
items.foreach(i => println(s.show(i)))
given Ordering[Product] = Ordering.by(_.price)
val products = List(Product("Laptop", 55000), Product("Mouse", 1400))
printAll(List(1, 2))
printAll(products.sorted) // sorted uses the given Ordering
#1 #2 Mouse (₹1,400) Laptop (₹55,000)
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Show[A] is a type class.
- given instances define Show for Int and Product.
- printAll asks for a Show[A] with using.
- sorted finds the given Ordering[Product].
Real-world use case#
JSON libraries (circe, upickle) use given encoders/decoders so case classes can be serialised automatically.
Common mistakes#
Two givens of the same type in scope cause a compile error. Keep one or import selectively.
Overuse makes code hard to follow. Pass normal parameters when clearer.
Best practices#
- Use for type classes and context
- Put givens in companion objects
- Import givens explicitly
- Avoid ambiguity
Remember this#
given = provide, using = require.
Type class = behaviour per type.
Scala 3 replaces implicit with given/using.
Practice exercise#
Your task: Provide a given Ordering[String] that sorts by length.
Show solution
given Ordering[String] = Ordering.by(_.length)
List("Scala", "Go", "Java").sorted // List(Go, Java, Scala)
Mini quiz#
Which Scala 3 keyword marks a parameter that the compiler fills automatically?
Want more? Practise all Scala MCQs · Scala interview questions
My notes#
Saved in this browser.