Skip to content

Commit 081ec82

Browse files
authored
Add derive via Generic example, and point to latest docs (documentationjs#331)
* Add derive via Generic example, and point to latest docs I often visit this page looking for a `genericShow` example. It was also frustrating to click on the `Data.Generic.Rep` and be taken to an older package version that's missing lots of nice documentation. So ensuring links now go to the latest version. (related to purescript/pursuit#414) * Clarify `derive` and more details on `Generic`
1 parent 04cfe07 commit 081ec82

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

language/Type-Classes.md

+20-6
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,27 @@ newtype Person = Person { name :: String, age :: Int }
145145
derive instance eqPerson :: Eq Person
146146
derive instance ordPerson :: Ord Person
147147
```
148-
Currently, the following type classes can be derived:
148+
Currently, the following type classes can be derived by the compiler:
149149

150-
- [Data.Generic.Rep (class Generic)](https://pursuit.purescript.org/packages/purescript-generics-rep/6.0.0/docs/Data.Generic.Rep#t:Generic)
151-
- [Data.Eq (class Eq)](https://pursuit.purescript.org/packages/purescript-prelude/4.1.0/docs/Data.Eq#t:Eq)
152-
- [Data.Ord (class Ord)](https://pursuit.purescript.org/packages/purescript-prelude/4.1.0/docs/Data.Ord#t:Ord)
153-
- [Data.Functor (class Functor)](https://pursuit.purescript.org/packages/purescript-prelude/4.1.0/docs/Data.Functor#t:Functor)
154-
- [Data.Newtype (class Newtype)](https://pursuit.purescript.org/packages/purescript-newtype/3.0.0/docs/Data.Newtype#t:Newtype)
150+
- [Data.Generic.Rep (class Generic)](https://pursuit.purescript.org/packages/purescript-generics-rep/docs/Data.Generic.Rep#t:Generic)
151+
- [Data.Eq (class Eq)](https://pursuit.purescript.org/packages/purescript-prelude/docs/Data.Eq#t:Eq)
152+
- [Data.Ord (class Ord)](https://pursuit.purescript.org/packages/purescript-prelude/docs/Data.Ord#t:Ord)
153+
- [Data.Functor (class Functor)](https://pursuit.purescript.org/packages/purescript-prelude/docs/Data.Functor#t:Functor)
154+
- [Data.Newtype (class Newtype)](https://pursuit.purescript.org/packages/purescript-newtype/docs/Data.Newtype#t:Newtype)
155+
156+
Note that `derive instance` is not the only mechanism for allowing you to avoid writing out boilerplate type class instance code. Many type classes not listed here can be derived through other means, such as via a Generic instance. For example, here's how to create a `Show` instance for `Person` via `genericShow`:
157+
158+
```purescript
159+
import Data.Generic.Rep (class Generic)
160+
import Data.Generic.Rep.Show (genericShow)
161+
162+
derive instance genericPerson :: Generic Person _
163+
164+
instance showPerson :: Show Person where
165+
show = genericShow
166+
```
167+
168+
More information on Generic deriving is available [in the generics-rep library documentation](https://pursuit.purescript.org/packages/purescript-generics-rep).
155169

156170
## Compiler-Solvable Type Classes
157171

0 commit comments

Comments
 (0)