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: language/Type-Classes.md
-35
Original file line number
Diff line number
Diff line change
@@ -258,41 +258,6 @@ main = logShow (Score 5)
258
258
259
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
260
261
-
#### Avoiding stack overflow errors with recursive types
262
-
263
-
Be careful when using generic functions with recursive data types. Due to strictness, these instances _cannot_ be written in point free style:
264
-
265
-
```purs
266
-
import Data.Generic.Rep (class Generic)
267
-
import Data.Generic.Rep.Show (genericShow)
268
-
import Effect.Console (logShow)
269
-
270
-
data Chain a
271
-
= End a
272
-
| Link a (Chain a)
273
-
274
-
derive instance genericChain :: Generic (Chain a) _
275
-
276
-
instance showChain :: Show a => Show (Chain a) where
277
-
show c = genericShow c -- Note the use of the seemingly-unnecessary variable `c`
278
-
279
-
main = logShow $ Link 1 $ Link 2 $ End 3
280
-
-- Prints:
281
-
-- (Link 1 (Link 2 (End 3)))
282
-
```
283
-
284
-
If the instance was written in point free style, then would produce a stack overflow error:
285
-
286
-
```purs
287
-
instance showChain :: Show a => Show (Chain a) where
288
-
show = genericShow -- This line is problematic
289
-
290
-
-- Throws this error:
291
-
-- RangeError: Maximum call stack size exceeded
292
-
```
293
-
294
-
This technique of undoing point free notation is known as _eta expansion_.
295
-
296
261
## Compiler-Solvable Type Classes
297
262
298
263
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