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/derivation.md
+11-11Lines changed: 11 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -3,15 +3,14 @@ layout: doc-page
3
3
title: Typeclass Derivation
4
4
---
5
5
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:
7
7
```scala
8
8
enumTree[T] derivesEq, Ordering, Pickling {
9
9
caseBranch(left: Tree[T], right: Tree[T])
10
10
caseLeaf(elem: T)
11
11
}
12
12
```
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`:
15
14
```scala
16
15
impl [T:Eq] of Eq[Tree[T]] =Eq.derived
17
16
impl [T:Ordering] of Ordering[Tree[T]] =Ordering.derived
@@ -39,21 +38,22 @@ case object None extends Option[Nothing]
39
38
40
39
The generated typeclass instances are placed in the companion objects `Labelled` and `Option`, respectively.
41
40
42
-
### Derivable Traits
41
+
### Derivable Types
43
42
44
-
A trait can appear in a `derives` clause as long as
43
+
A trait or class can appear in a `derives` clause if
45
44
46
-
- it has a single type parameter,
45
+
- it has a single type parameter, and
47
46
- its companion object defines a method named `derived`.
48
47
49
48
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
51
50
defderived[T] with (ev: Generic[T]) = ...
52
51
```
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
+
sealedtraitParseResult[T] derivesGeneric
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.
0 commit comments