Skip to content

Commit f93026b

Browse files
committed
Reorder introduction of #exact-mechanism
1 parent f1036d7 commit f93026b

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

docs/_docs/reference/contextual/derivation.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,27 @@ 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:** `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:
30+
31+
```scala
32+
given [T: Ordering]: Ordering[Option[T]] = Ordering.derived
33+
```
34+
35+
It is discouraged to directly refer to the `derived` member if you can use a `derives` clause instead.
36+
2937
## Exact mechanism
30-
More formally, for a class/trait/object/enum `DerivingType[T_1, ..., T_N] derives TC`, a derived instance is created in `DerivingType`'s companion object (or `DerivingType` itself if it is an object).
38+
For a class/trait/object/enum `DerivingType[T_1, ..., T_N] derives TC`, a derived instance is created in `DerivingType`'s companion object (or `DerivingType` itself if it is an object).
3139
If `DerivingType` does not take parameters, we define `N = 0`.
3240

33-
What the derived instance looks like depends on the specifics of `DerivingType` and `TC`, but the general shape is as follows:
41+
The general "shape" of the derived instance is as follows:
3442
```scala
3543
given [...]: TC[ ... DerivingType[...] ... ] = TC.derived
3644
```
45+
`TC.derived` should be an expression that conforms to the expected type on the left, potentially elaborated using term and/or type inference.
3746

38-
The first condition is the arity of `TC`:
47+
**Note:** `TC.derived` is a normal access, therefore if there are multiple definitions of `TC.derived`, overloading resolution applies.
48+
49+
What the derived instance precisely looks like depends on the specifics of `DerivingType` and `TC`, first condition the arity of `TC`:
3950

4051
### `TC` takes 1 parameter
4152

@@ -107,18 +118,6 @@ The bounds of `T_i`s are handled correctly, for example: `T_2 <: T_1` becomes `T
107118

108119
Throw some error.
109120

110-
`TC.derived` should be an expression that conforms to the expected type `TC[DerivingType]`, potentially elaborated using term and/or type inference.
111-
112-
**Note:** `TC.derived` is a normal access, therefore if there are multiple definitions of `TC.derived`, overloading resolution applies.
113-
114-
**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:
115-
116-
```scala
117-
given [T: Ordering]: Ordering[Option[T]] = Ordering.derived
118-
```
119-
120-
It is discouraged to directly refer to the `derived` member if you can use a `derives` clause instead.
121-
122121
All data types can have a `derives` clause. The rest of this document focuses primarily on data types which also have a given instance
123122
of the `Mirror` type class available.
124123

0 commit comments

Comments
 (0)