Skip to content

Commit e0ffb4f

Browse files
committed
Update derivation.md
1 parent eebc7f2 commit e0ffb4f

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

docs/_docs/reference/contextual/derivation.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,39 @@ given [T: Show] : Show[Tree[T]] = Show.derived
2626

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

29-
**Note:** The access to `derived` above is a normal access, therefore if there are multiple definitions of `derived` in the type class, overloading resolution applies.
29+
More formally, for a class/trait/object/enum `DerivingType derives TC`, the following given instance is created in `DerivingType`'s companion object (or `DerivingType` itself if it is an object):
30+
* if `DerivingType` doesn't have type parameters
31+
```scala
32+
given TC[DerivingType] = TC.derived
33+
```
34+
* if `DerivingType` has type parameters `[T_1, ..., T_N]`
35+
```scala
36+
given [T_1: TC, ... T_N: TC]: TC[DerivingType[T_1, ..., T_N]] = TC.derived
37+
```
38+
39+
`TC.derived` should be an expression that conforms to the expected type `TC[DerivingType]`, potentially elaborated using term and/or type inference.
40+
41+
**Note:** `TC.derived` is a normal access, therefore if there are multiple definitions of `TC.derived`, overloading resolution applies.
3042

31-
**Note:** `derived` can be used manually, this is useful when you do not have control over the definition. For example we can implement an `Ordering` for `Option`s like so:
43+
**Note:** `TC.derived` can be used manually, this is useful when you do not have control over the definition. For example we can implement `Ordering` for `Option`s like so:
3244

3345
```scala
3446
given [T: Ordering]: Ordering[Option[T]] = Ordering.derived
3547
```
3648

3749
It is discouraged to directly refer to the `derived` member if you can use a `derives` clause instead.
3850

51+
#### CanEqual
52+
53+
With `DerivingType[T_1, ..., T_N] derives CanEqual`, the following instance is created:
54+
55+
```scala
56+
given [T_1L, T_1R, ..., T_NL, T_NR]
57+
(using CanEqual[T_1L, T_1R], ..., CanEqual[T_NL, T_NR]):
58+
CanEqual[DerivingType[T_1L, ..., T_NL], DerivingType[T_1R, ..., T_NR]] =
59+
CanEqual.derived
60+
```
61+
3962
All data types can have a `derives` clause. This document focuses primarily on data types which also have a given instance
4063
of the `Mirror` type class available.
4164

0 commit comments

Comments
 (0)