Skip to content

Commit ec9541f

Browse files
committed
document mirror changes
1 parent c47b6b8 commit ec9541f

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

docs/_docs/reference/contextual/derivation-macro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ given derived[T: Type](using Quotes): Expr[Eq[T]]
3131
and for comparison reasons we give the same signature we had with `inline`:
3232

3333
```scala
34-
inline given derived[T]: (m: Mirror.Of[T]) => Eq[T] = ???
34+
inline given derived[T](using Mirror.Of[T]): Eq[T] = ???
3535
```
3636

3737
Note, that since a type is used in a subsequent stage it will need to be lifted

docs/_docs/reference/contextual/derivation.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,28 @@ We say that `Tree` is the _deriving type_ and that the `Eq`, `Ordering` and `Sho
2929
### Types supporting `derives` clauses
3030

3131
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.
3833

3934
`Mirror` type class instances provide information at the type level about the components and labelling of the type.
4035
They also provide minimal term level infrastructure to allow higher level libraries to provide comprehensive
4136
derivation support.
4237

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+
4354
```scala
4455
sealed trait Mirror:
4556

@@ -119,11 +130,11 @@ new Mirror.Product:
119130
new Leaf(...)
120131
```
121132

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
123134
sum type nor a product type. For example, if `A` is a trait that is not sealed,
124135

125136
```
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]:
127138
* trait A is not a generic product because it is not a case class
128139
* trait A is not a generic sum because it is not a sealed trait
129140
```
@@ -133,6 +144,7 @@ Note the following properties of `Mirror` types,
133144

134145
+ Properties are encoded using types rather than terms. This means that they have no runtime footprint unless used and
135146
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.
136148
+ The kinds of `MirroredType` and `MirroredElemTypes` match the kind of the data type the mirror is an instance for.
137149
This allows `Mirror`s to support ADTs of all kinds.
138150
+ There is no distinct representation type for sums or products (ie. there is no `HList` or `Coproduct` type as in
@@ -155,7 +167,7 @@ following form,
155167
```scala
156168
import scala.deriving.Mirror
157169

158-
def derived[T](using Mirror.Of[T]): TC[T] = ...
170+
inline def derived[T](using Mirror.Of[T]): TC[T] = ...
159171
```
160172

161173
That is, the `derived` method takes a context parameter of (some subtype of) type `Mirror` which defines the shape of

0 commit comments

Comments
 (0)