Skip to content

Commit 9409b9a

Browse files
authored
Remove content on deriving from Generic
1 parent bb30b15 commit 9409b9a

File tree

1 file changed

+0
-59
lines changed

1 file changed

+0
-59
lines changed

language/Type-Classes.md

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -199,65 +199,6 @@ derive instance eqScore :: Eq Score
199199
derive newtype instance eqScore :: Eq Score
200200
```
201201

202-
### Deriving from `Generic`
203-
204-
The compiler's built-in support for `Generic` unlocks convenient deriving for many other classes not listed above.
205-
206-
For example, if we wanted to derive a `Show` instance for `MyADT` it might seem like we're out of luck: `Show` is not a class with built-in compiler support for deriving and `MyADT` is not a `newtype` (so we can't use newtype deriving).
207-
208-
But we _can_ use `genericShow`, which works with _any_ type that has a `Generic` instance. And recall that the compiler has built-in support for deriving a `Generic` instance for any type (including the `MyADT` type). We put all those pieces together like so:
209-
210-
```purescript
211-
import Data.Generic.Rep (class Generic)
212-
import Data.Generic.Rep.Show (genericShow)
213-
import Effect.Console (logShow)
214-
215-
derive instance genericMyADT :: Generic MyADT _
216-
217-
instance showMyADT :: Show MyADT where
218-
show = genericShow
219-
220-
main = logShow [Some, Arbitrary 1, Contents 2.0 "Three"]
221-
-- Prints:
222-
-- [Some,(Arbitrary 1),(Contents 2.0 "Three")]
223-
```
224-
225-
The `Show` type class is most often used for debugging data, so the output of most `Show` instances can be copy-pasted back into a PureScript source file to reconstruct the original data. The `Show` instance we created by deriving `Generic` and then using `genericShow` follows this convention.
226-
227-
This is a good opportunity to emphasize how newtype deriving is different from instances derived by the compiler or through the `Generic` type class. In the examples below, notice how the instance derived through `Generic` includes the newtype constructor `Score`, but the newtype-derived instance simply reuses the underlying `Show` instance for `Int` and therefore does not include the constructor:
228-
229-
```purs
230-
import Effect.Console (logShow)
231-
232-
newtype Score = Score Int
233-
234-
-- newtype deriving omits wrapper with show
235-
derive newtype instance showScore :: Show Score
236-
237-
main = logShow (Score 5)
238-
-- Prints:
239-
-- 5
240-
```
241-
242-
```purs
243-
import Data.Generic.Rep (class Generic)
244-
import Data.Generic.Rep.Show (genericShow)
245-
import Effect.Console (logShow)
246-
247-
newtype Score = Score Int
248-
249-
-- generic deriving prints wrapper with show
250-
derive instance genericScore :: Generic Score _
251-
instance showScore :: Show Score where
252-
show = genericShow
253-
254-
main = logShow (Score 5)
255-
-- Prints:
256-
-- (Score 5)
257-
```
258-
259-
More information on Generic deriving is available [in the generics-rep library documentation](https://pursuit.purescript.org/packages/purescript-generics-rep). See this [blog post](https://harry.garrood.me/blog/write-your-own-generics/) for a tutorial on how to write your own `generic` functions.
260-
261202
## Compiler-Solvable Type Classes
262203

263204
Some type classes can be automatically solved by the PureScript Compiler without requiring you place a PureScript statement, like `derive instance`, in your source code.

0 commit comments

Comments
 (0)