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
Type class derivation is a way to automatically generate given instances for type classes which satisfy some simple
8
-
conditions. A type class in this sense is any trait or class with a type parameter determining the type being operated
9
-
on. Common examples are `Eq`, `Ordering`, or `Show`. For example, given the following `Tree` algebraic data type
8
+
conditions. A type class in this sense is any trait or class with a single type parameter determining the type being operated
9
+
on, and the special case `CanEqual`. Common examples are `Eq`, `Ordering`, or `Show`. For example, given the following `Tree` algebraic data type
10
10
(ADT):
11
11
12
12
```scala
@@ -35,7 +35,7 @@ given [T: Ordering]: Ordering[Option[T]] = Ordering.derived
35
35
It is discouraged to directly refer to the `derived` member if you can use a `derives` clause instead.
36
36
37
37
## Exact mechanism
38
-
In the following, a parameter enumerations where the first index is bigger than the last means there is actually no paramers, for example: `A[T_2, ..., T_1]` means `A`.
38
+
In the following, when type arguments are enumerated and the first index evaluates to a larger value than the last, then there are actually no arguments, for example: `A[T_2, ..., T_1]` means `A`.
39
39
40
40
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).
**Note:**`TC.derived` is a normal access, therefore if there are multiple definitions of `TC.derived`, overloading resolution applies.
49
49
50
-
What the derived instance precisely looks like depends on the specifics of `DerivingType` and `TC`, the first condition is the arity of`TC`:
50
+
What the derived instance precisely looks like depends on the specifics of `DerivingType` and `TC`, we first examine`TC`:
51
51
52
52
### `TC` takes 1 parameter
53
53
54
-
Therefore `TC` is defined as `TC[F[A_1, ..., A_K]]` (`TC[F]` if `K == 0`), there are two further cases depending on the `A_i`s:
54
+
Therefore `TC` is defined as `TC[F[A_1, ..., A_K]]` (`TC[F]` if `K == 0`) for some `F`.
55
+
There are two further cases depending on the kinds of arguments:
55
56
56
57
#### `F` and all arguments of `DerivingType` have kind `*`
57
58
@@ -60,18 +61,31 @@ The generated instance is then:
60
61
given [T_1:TC, ..., T_N:TC]:TC[DerivingType[T_1, ..., T_N]] =TC.derived
61
62
```
62
63
64
+
This is the most common case, and is the one that was highlighted in the introduction.
65
+
63
66
**Note:** If `N == 0` the above means:
64
67
```scala
65
68
givenTC[DerivingType] =TC.derived
66
69
```
70
+
For example, the class
71
+
```scala
72
+
caseclassPoint(x: Int, y: Int) derivesOrdering
73
+
```
74
+
generates the instance
75
+
```scala
76
+
objectPoint:
77
+
...
78
+
givenOrdering[Point] =Ordering.derived
79
+
```
80
+
67
81
68
82
#### `F` and `DerivingType` have parameters of matching kind on the right
69
-
This section concers cases where you can pair arguments of `F` and `DerivingType` starting from the right such that they have the same kinds pairwise, and all arguments of `F` or `DerivingType` (or both) are used up.
83
+
This section covers cases where you can pair arguments of `F` and `DerivingType` starting from the right such that they have the same kinds pairwise, and all arguments of `F` or `DerivingType` (or both) are used up.
70
84
`F` must also have at least one parameter.
71
85
72
86
The general shape will then be:
73
87
```scala
74
-
given [...]:TC[ [...] =>DerivingType[...] ] =TC.derived
88
+
given [...]:TC[ [...] =>>DerivingType[...] ] =TC.derived
75
89
```
76
90
Where of course `TC` and `DerivingType` are applied to types of the correct kind.
77
91
@@ -96,10 +110,7 @@ If `TC` takes less arguments than `DerivingType` (`K < N`), we fill in the leftm
0 commit comments