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
@@ -158,7 +158,7 @@ of the `Mirror` type class available.
158
158
## `Mirror`
159
159
160
160
`scala.deriving.Mirror` type class instances provide information at the type level about the components and labelling of the type.
161
-
They also provide minimal termlevel infrastructure to allow higherlevel libraries to provide comprehensive
161
+
They also provide minimal term-level infrastructure to allow higher-level libraries to provide comprehensive
162
162
derivation support.
163
163
164
164
Instances of the `Mirror` type class are generated automatically by the compiler
@@ -269,14 +269,14 @@ No given instance of type deriving.Mirror.Of[A] was found for parameter x of met
269
269
Note the following properties of `Mirror` types,
270
270
271
271
+ Properties are encoded using types rather than terms. This means that they have no runtime footprint unless used and
272
-
also that they are a compiletime feature for use with Scala 3's metaprogramming facilities.
272
+
also that they are a compile-time feature for use with Scala 3's metaprogramming facilities.
273
273
+ There is no restriction against the mirrored type being a local or inner class.
274
274
+ The kinds of `MirroredType` and `MirroredElemTypes` match the kind of the data type the mirror is an instance for.
275
275
This allows `Mirror`s to support ADTs of all kinds.
276
276
+ There is no distinct representation type for sums or products (ie. there is no `HList` or `Coproduct` type as in
277
277
Scala 2 versions of Shapeless). Instead the collection of child types of a data type is represented by an ordinary,
278
278
possibly parameterized, tuple type. Scala 3's metaprogramming facilities can be used to work with these tuple types
279
-
as-is, and higherlevel libraries can be built on top of them.
279
+
as-is, and higher-level libraries can be built on top of them.
280
280
+ For both product and sum types, the elements of `MirroredElemTypes` are arranged in definition order (i.e. `Branch[T]`
281
281
precedes `Leaf[T]` in `MirroredElemTypes` for `Tree` because `Branch` is defined before `Leaf` in the source file).
282
282
This means that `Mirror.Sum` differs in this respect from Shapeless's generic representation for ADTs in Scala 2,
@@ -303,16 +303,16 @@ has a context `Mirror` parameter, or not at all (e.g. they might use some comple
303
303
instance using Scala 3 macros or runtime reflection). We expect that (direct or indirect) `Mirror` based implementations
304
304
will be the most common and that is what this document emphasises.
305
305
306
-
Type class authors will most likely use higherlevel derivation or generic programming libraries to implement
307
-
`derived` methods. An example of how a `derived` method might be implemented using _only_ the lowlevel facilities
306
+
Type class authors will most likely use higher-level derivation or generic programming libraries to implement
307
+
`derived` methods. An example of how a `derived` method might be implemented using _only_ the low-level facilities
308
308
described above and Scala 3's general metaprogramming features is provided below. It is not anticipated that type class
309
309
authors would normally implement a `derived` method in this way, however this walkthrough can be taken as a guide for
310
-
authors of the higherlevel derivation libraries that we expect typical type class authors will use (for a fully
310
+
authors of the higher-level derivation libraries that we expect typical type class authors will use (for a fully
311
311
worked out example of such a library, see [Shapeless 3](https://github.com/milessabin/shapeless/tree/shapeless-3)).
312
312
313
-
## How to write a type class `derived` method using lowlevel mechanisms
313
+
## How to write a type class `derived` method using low-level mechanisms
314
314
315
-
The low-level method we will use to implement a type class `derived` method in this example exploits three new type-level constructs in Scala 3: inline methods, inline matches, and implicit searches via `summonInline` or `summonFrom`.
315
+
The low-level technique we will use to implement a type class `derived` method in this example exploits three new type-level constructs in Scala 3: inline methods, inline matches, and implicit searches via `summonInline` or `summonFrom`.
Note that `derived` is defined as an `inline def`.
338
-
This means that the method will be inlined at all call sites (for instance the compilergenerated instance definitions in the companion objects of ADTs which have a `deriving Eq` clause).
338
+
This means that the method will be inlined at all call sites (for instance the compiler-generated instance definitions in the companion objects of ADTs which have a `deriving Eq` clause).
339
339
340
340
> Inlining of complex code is potentially expensive if overused (meaning slower compile times) so we should be careful to limit how many times `derived` is called for the same type.
341
-
> For example, when computing an instance for a sum type, it may be necessary to call `derived` recursively to compute an instance for a one of its child cases.
341
+
> For example, when computing an instance for a sum type, it may be necessary to call `derived` recursively to compute an instance for each one of its child cases.
342
342
> That child case may in turn be a product type, that declares a field referring back to the parent sum type.
343
343
> To compute the instance for this field, we should not call `derived` recursively, but instead summon from the context.
344
-
> Typically the found given instance will be the root given instance that initially called `derived`.
344
+
> Typically, the found given instance will be the root given instance that initially called `derived`.
345
345
346
346
The body of `derived` (1) first materializes the `Eq` instances for all the child types of type the instance is being derived for.
347
347
This is either all the branches of a sum type or all the fields of a product type.
(s.ordinal(y) == ordx) && check(x, y, elems(ordx)) // (4)
381
381
```
382
382
383
-
In the product case, `eqProduct` we test the runtime values of the arguments to `eqv` for equality as products based on the `Eq` instances for the fields of the data type (5),
383
+
In the product case, `eqProduct`, we test the runtime values of the arguments to `eqv` for equality as products based on the `Eq` instances for the fields of the data type (5),
384
384
385
385
```scala
386
386
importscala.deriving.Mirror
@@ -486,7 +486,7 @@ Alternative approaches can be taken to the way that `derived` methods can be def
486
486
inlined variants using Scala 3 macros, whilst being more involved for type class authors to write than the example
487
487
above, can produce code for type classes like `Eq` which eliminate all the abstraction artefacts (eg. the `Lists` of
488
488
child instances in the above) and generate code which is indistinguishable from what a programmer might write by hand.
489
-
As a third example, using a higherlevel library such as Shapeless the type class author could define an equivalent
489
+
As a third example, using a higher-level library such as Shapeless, the type class author could define an equivalent
0 commit comments