Scala · OOP & Functional Programming

Given and Using

given and using let the compiler pass context (like a formatter or configuration) automatically, enabling type classes.

Advanced 5 min Lesson 18 of 25Examples use Scala 3 (3.3 LTS or newer) with sbt or scala-cli.
0/25 in Scala
On this page · 5 min · Advanced

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#

ScalaA Show type class Illustrative
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
Expected output
#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#

  1. Show[A] is a type class.
  2. given instances define Show for Int and Product.
  3. printAll asks for a Show[A] with using.
  4. 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#

Ambiguous givens

Two givens of the same type in scope cause a compile error. Keep one or import selectively.

Using givens for ordinary configuration everywhere

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
Scala Illustrative
given Ordering[String] = Ordering.by(_.length)
List("Scala", "Go", "Java").sorted  // List(Go, Java, Scala)

Mini quiz#

Given and Using quizMultiple choice Question 1 of 1

Which Scala 3 keyword marks a parameter that the compiler fills automatically?

Score so far: 0/0

Want more? Practise all Scala MCQs · Scala interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.