Skip to content

Commit bb30b15

Browse files
authored
Remove guide on how to avoid stack overflow errors with recursive types
1 parent 66fcdc2 commit bb30b15

File tree

1 file changed

+0
-35
lines changed

1 file changed

+0
-35
lines changed

language/Type-Classes.md

-35
Original file line numberDiff line numberDiff line change
@@ -258,41 +258,6 @@ main = logShow (Score 5)
258258

259259
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.
260260

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-
296261
## Compiler-Solvable Type Classes
297262

298263
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)