Skip to content

Commit 3f917bb

Browse files
committed
Tweaks to docs
1 parent c2f96d2 commit 3f917bb

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

docs/docs/reference/derivation.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ layout: doc-page
33
title: Typeclass Derivation
44
---
55

6-
Implicit instances for some typeclass traits can be derived automatically. Example:
6+
Typeclass derivation is a way to generate instances of certain type classes automatically or with minimal code hints. A type class in this sense is any trait or class with a type parameter that describes the type being operated on. Commonly used examples are `Eq`, `Ordering`, `Show`, or `Pickling`. Example:
77
```scala
88
enum Tree[T] derives Eq, Ordering, Pickling {
99
case Branch(left: Tree[T], right: Tree[T])
1010
case Leaf(elem: T)
1111
}
1212
```
13-
The derives clause automatically generates typeclass instances for
14-
`Eq`, `Ordering`, and `Pickling` in the companion object `Tree`:
13+
The `derives` clause generates implicit instances of the `Eq`, `Ordering`, and `Pickling` traits in the companion object `Tree`:
1514
```scala
1615
impl [T: Eq] of Eq[Tree[T]] = Eq.derived
1716
impl [T: Ordering] of Ordering[Tree[T]] = Ordering.derived
@@ -39,21 +38,22 @@ case object None extends Option[Nothing]
3938

4039
The generated typeclass instances are placed in the companion objects `Labelled` and `Option`, respectively.
4140

42-
### Derivable Traits
41+
### Derivable Types
4342

44-
A trait can appear in a `derives` clause as long as
43+
A trait or class can appear in a `derives` clause if
4544

46-
- it has a single type parameter,
45+
- it has a single type parameter, and
4746
- its companion object defines a method named `derived`.
4847

4948
These two conditions ensure that the synthesized derived instances for the trait are well-formed. The type and implementation of a `derived` method are arbitrary, but typically it has a definition like this:
50-
```
49+
```scala
5150
def derived[T] with (ev: Generic[T]) = ...
5251
```
53-
That is, the `derived` method takes an implicit parameter of type `Generic` that determines the _shape_ of the deriving type `T` and it computes the typeclass implementation according to that shape. Implicit `Generic` instances are generated automatically for all types that have a `derives` clause.
54-
55-
This is all a user of typeclass derivation has to know. The rest of this page contains information needed to be able to write a typeclass that can be used in a `derives` clause. In particular, it details the means provided for the implementation of data generic `derived` methods.
56-
52+
That is, the `derived` method takes an implicit parameter of type `Generic` that determines the _shape_ of the deriving type `T` and it computes the typeclass implementation according to that shape. Implicit `Generic` instances are generated automatically for all types that have a `derives` clause. One can also derive `Generic` alone, which means a `Generic` instance is generated without any other type class instances. E.g.:
53+
```scala
54+
sealed trait ParseResult[T] derives Generic
55+
```
56+
This is all a user of typeclass derivation has to know. The rest of this page contains information needed to be able to write a typeclass that can appear in a `derives` clause. In particular, it details the means provided for the implementation of data generic `derived` methods.
5757

5858
### The Shape Type
5959

0 commit comments

Comments
 (0)