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
Copy file name to clipboardExpand all lines: docs/_docs/reference/contextual/derivation.md
+21-9Lines changed: 21 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -29,17 +29,28 @@ We say that `Tree` is the _deriving type_ and that the `Eq`, `Ordering` and `Sho
29
29
### Types supporting `derives` clauses
30
30
31
31
All data types can have a `derives` clause. This document focuses primarily on data types which also have a given instance
32
-
of the `Mirror` type class available. Instances of the `Mirror` type class are generated automatically by the compiler
33
-
for,
34
-
35
-
+ enums and enum cases
36
-
+ case classes and case objects
37
-
+ sealed classes or traits that have only case classes and case objects as children
32
+
of the `Mirror` type class available.
38
33
39
34
`Mirror` type class instances provide information at the type level about the components and labelling of the type.
40
35
They also provide minimal term level infrastructure to allow higher level libraries to provide comprehensive
41
36
derivation support.
42
37
38
+
Instances of the `Mirror` type class are generated automatically by the compiler
39
+
unconditionally for:
40
+
- enums and enum cases,
41
+
- case objects.
42
+
43
+
Instances for `Mirror` are also generated conditionally for:
44
+
- case classes where the constructor is visible at the callsite (always true if the companion is not a case object)
45
+
- sealed classes and sealed traits where:
46
+
- there exists at least one child case,
47
+
- each child case is reachable from the parent's definition,
48
+
- 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,
49
+
- and where the compiler can generate a `Mirror` type class instance for each child case.
50
+
51
+
52
+
The `Mirror` type class definition is as follows:
53
+
43
54
```scala
44
55
sealedtraitMirror:
45
56
@@ -119,11 +130,11 @@ new Mirror.Product:
119
130
newLeaf(...)
120
131
```
121
132
122
-
If a Mirror cannot be generated automatically for a given type, an error will appear explaining why it is neither a supported
133
+
If a Mirror cannot be generated automatically for a given type, an error will appear explaining why it is neither a supported
123
134
sum type nor a product type. For example, if `A` is a trait that is not sealed,
124
135
125
136
```
126
-
No given instance of type deriving.Mirror.Of[A] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[A]:
137
+
No given instance of type deriving.Mirror.Of[A] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[A]:
127
138
* trait A is not a generic product because it is not a case class
128
139
* trait A is not a generic sum because it is not a sealed trait
129
140
```
@@ -133,6 +144,7 @@ Note the following properties of `Mirror` types,
133
144
134
145
+ Properties are encoded using types rather than terms. This means that they have no runtime footprint unless used and
135
146
also that they are a compile time feature for use with Scala 3's metaprogramming facilities.
147
+
+ There is no restriction against the mirrored type being a local or inner class.
136
148
+ The kinds of `MirroredType` and `MirroredElemTypes` match the kind of the data type the mirror is an instance for.
137
149
This allows `Mirror`s to support ADTs of all kinds.
138
150
+ There is no distinct representation type for sums or products (ie. there is no `HList` or `Coproduct` type as in
0 commit comments