You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We say that `Tree` is the _deriving type_ and that the `Eq`, `Ordering` and `Show` instances are _derived instances_.
28
28
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
+
29
37
## 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).
31
39
If `DerivingType` does not take parameters, we define `N = 0`.
32
40
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:
34
42
```scala
35
43
given [...]:TC[ ... DerivingType[...] ... ] =TC.derived
36
44
```
45
+
`TC.derived` should be an expression that conforms to the expected type on the left, potentially elaborated using term and/or type inference.
37
46
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`:
39
50
40
51
### `TC` takes 1 parameter
41
52
@@ -107,18 +118,6 @@ The bounds of `T_i`s are handled correctly, for example: `T_2 <: T_1` becomes `T
107
118
108
119
Throw some error.
109
120
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
-
122
121
All data types can have a `derives` clause. The rest of this document focuses primarily on data types which also have a given instance
0 commit comments