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
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
-
261
202
## Compiler-Solvable Type Classes
262
203
263
204
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