Givens in Scala 3 as behaviours injected into functions
Note
What is the use case of givens
in Scala 3? They are a replacement for implicits
, avoiding to explicitly have to pass instances used at call site. Given
is well named - it is considered a "given" to the function. We don't instantiate it as we do with implicits, but rather automatically creates instances to be injected in the function at call site.
An example of a given
instance that defines an ordering behaviour for a custom type Person
:
given personOrdering as Ordering[Person] {
override def compare(x: Person, y: Person): Int =
x.surname.compareTo(y.surname)
}
And then in the function to use it:
def listPeople(people: List[Person])(using ordering: Ordering[Person]) = ???
This way we don't need to explicitly pass the ordering
argument. We are using
the ordering behaviour we have previously defined for the Person
type. This encodes the concept of type classes, e.g. behaviours for types.