<commit_before>---
layout: doc-page
title: "Type Class Derivation"
nightlyOf: https://docs.scala-lang.org/scala3/reference/contextual/derivation.html
---

Type class derivation is a way to automatically generate given instances for type classes which satisfy some simple
conditions. A type class in this sense is any trait or class with a type parameter determining the type being operated
on. Common examples are `Eq`, `Ordering`, or `Show`. For example, given the following `Tree` algebraic data type
(ADT),

```scala
enum Tree[T] derives Eq, Ordering, Show:
  case Branch(left: Tree[T], right: Tree[T])
  case Leaf(elem: T)
```

The `derives` clause generates the following given instances for the `Eq`, `Ordering` and `Show` type classes in the
companion object of `Tree`,

```scala
given [T: Eq]       : Eq[Tree[T]]    = Eq.derived
given [T: Ordering] : Ordering[Tree] = Ordering.derived
given [T: Show]     : Show[Tree]     = Show.derived
```

We say that `Tree` is the _deriving type_ and that the `Eq`, `Ordering` and `Show` instances are _derived instances_.

### Types supporting `derives` clauses

All data types can have a `derives` clause. This document focuses primarily on data types which also have a given instance
of the `Mirror` type class available. Instances of the `Mirror` type class are generated automatically by the compiler
for,<commit_msg>Typo: add T type param<commit_after>---
layout: doc-page
title: "Type Class Derivation"
nightlyOf: https://docs.scala-lang.org/scala3/reference/contextual/derivation.html
---

Type class derivation is a way to automatically generate given instances for type classes which satisfy some simple
conditions. A type class in this sense is any trait or class with a type parameter determining the type being operated
on. Common examples are `Eq`, `Ordering`, or `Show`. For example, given the following `Tree` algebraic data type
(ADT),

```scala
enum Tree[T] derives Eq, Ordering, Show:
  case Branch(left: Tree[T], right: Tree[T])
  case Leaf(elem: T)
```

The `derives` clause generates the following given instances for the `Eq`, `Ordering` and `Show` type classes in the
companion object of `Tree`,

```scala
given [T: Eq]       : Eq[Tree[T]]       = Eq.derived
given [T: Ordering] : Ordering[Tree[T]] = Ordering.derived
given [T: Show]     : Show[Tree[T]]     = Show.derived
```

We say that `Tree` is the _deriving type_ and that the `Eq`, `Ordering` and `Show` instances are _derived instances_.

### Types supporting `derives` clauses

All data types can have a `derives` clause. This document focuses primarily on data types which also have a given instance
of the `Mirror` type class available. Instances of the `Mirror` type class are generated automatically by the compiler
for,
{'fraction_non_alphanumeric': 0.09920058139534883, 'fraction_numerical': 0.0007267441860465116, 'mean_word_length': 4.528112449799197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '124599', 'n_tokens_mistral': 815, 'n_tokens_neox': 764, 'n_words': 367}