Skip to content

Commit 535cf08

Browse files
committed
Add CanEqual section to derivation.md, Small fixes
1 parent fcefd2b commit 535cf08

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

docs/_docs/reference/contextual/derivation.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,56 @@ More formally, for a class/trait/object/enum `DerivingType derives TC`, the foll
3131
```scala
3232
given TC[DerivingType] = TC.derived
3333
```
34-
* if `DerivingType` has type parameters `[T_1, ... T_N]`
34+
* if `DerivingType` has type parameters `[T_1, ..., T_N]`
3535
```scala
36-
given [T_1: TC, ... T_N: TC]: TC[DerivingType[T_1, ... T_N]] = TC.derived
36+
given [T_1: TC, ... T_N: TC]: TC[DerivingType[T_1, ..., T_N]] = TC.derived
3737
```
3838

3939
`TC.derived` should be an expression that conforms to the expected type `TC[DerivingType]`, potentially elaborated using term and/or type inference.
4040

41-
**Note:** `TC.derived` is a normal method call, therefore if there are multiple definitions of `TC.derived`, overloading resolution applies.
41+
**Note:** `TC.derived` is a normal access, therefore if there are multiple definitions of `TC.derived`, overloading resolution applies.
4242

43-
**Note:** `TC.derived` can be used manually, for example to implement `Ordering` for `Option`:
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:
4444

4545
```scala
4646
given [T: Ordering]: Ordering[Option[T]] = Ordering.derived
4747
```
4848

49-
### `Mirror`
49+
It is discouraged to use `derived` if you can use a `derives` clause instead.
50+
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+
62+
## `Mirror`
5063

5164

5265
`Mirror` instances provide information at the type level about the components and labelling of the type.
5366
They also provide minimal term level infrastructure to allow higher level libraries to provide comprehensive
5467
derivation support.
5568

56-
Instances of `Mirror` are generated automatically by the compiler
69+
Instances of the `Mirror` type class are generated automatically by the compiler
5770
unconditionally for:
5871
- enums and enum cases,
5972
- case objects.
6073

61-
Instances of `Mirror` are also generated conditionally for:
74+
Instances for `Mirror` are also generated conditionally for:
6275
- case classes where the constructor is visible at the callsite (always true if the companion is not a case object)
6376
- sealed classes and sealed traits where:
6477
- there exists at least one child case,
6578
- each child case is reachable from the parent's definition,
6679
- if the sealed trait/class has no companion, then each child case is reachable from the callsite through the prefix of the type being mirrored,
67-
- and where the compiler can generate a `Mirror` instance for each child case.
80+
- and where the compiler can generate a `Mirror` type class instance for each child case.
6881

6982

70-
The `Mirror` definition is as follows:
83+
The `Mirror` type class definition is as follows:
7184

7285
```scala
7386
sealed trait Mirror:
@@ -374,7 +387,7 @@ The framework described here enables all three of these approaches without manda
374387
For a brief discussion on how to use macros to write a type class `derived`
375388
method please read more at [How to write a type class `derived` method using macros](./derivation-macro.md).
376389

377-
### Syntax
390+
## Syntax
378391

379392
```
380393
Template ::= InheritClauses [TemplateBody]
@@ -397,7 +410,7 @@ It is equivalent to the old form
397410
class A extends B with C { ... }
398411
```
399412

400-
### Discussion
413+
## Discussion
401414

402415
This type class derivation framework is intentionally very small and low-level. There are essentially two pieces of
403416
infrastructure in compiler-generated `Mirror` instances,

0 commit comments

Comments
 (0)