Skip to content

Commit 44013c6

Browse files
authored
[Chapter 6] Add a note on optional type class instance names (#461)
1 parent 874eefe commit 44013c6

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

text/chapter6.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,47 @@ instance Show Boolean where
4747
show false = "false"
4848
```
4949

50-
This code declares a type class instance called `showBoolean` – in PureScript, type class instances can be named to aid the readability of the generated JavaScript. We say that the `Boolean` type _belongs to the `Show` type class_.
51-
52-
We can try out the `Show` type class in PSCi, by showing a few values with different types:
50+
This code declares a type class instance; we say that the `Boolean` type _belongs to the `Show` type class_.
51+
52+
> If you're wondering, the generated JS code looks like this:
53+
>
54+
> ```javascript
55+
> var showBoolean = {
56+
> show: function (v) {
57+
> if (v) {
58+
> return "true";
59+
> };
60+
> if (!v) {
61+
> return "false";
62+
> };
63+
> throw new Error("Failed pattern match at ...");
64+
> }
65+
> };
66+
> ```
67+
>
68+
> If you're unhappy with the generated name, you can give names to type class instances. For example:
69+
>
70+
> ```haskell
71+
> instance myShowBoolean :: Show Boolean where
72+
> show true = "true"
73+
> show false = "false"
74+
> ```
75+
>
76+
> ```javascript
77+
> var myShowBoolean = {
78+
> show: function (v) {
79+
> if (v) {
80+
> return "true";
81+
> };
82+
> if (!v) {
83+
> return "false";
84+
> };
85+
> throw new Error("Failed pattern match at ...");
86+
> }
87+
> };
88+
> ```
89+
90+
We can try out the `Show` type class in PSCi by showing a few values with different types:
5391
5492
```text
5593
> import Prelude

0 commit comments

Comments
 (0)