Skip to content

Commit

Permalink
[Chapter 6] Add a note on optional type class instance names (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zelenya authored Nov 30, 2023
1 parent 874eefe commit 44013c6
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions text/chapter6.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,47 @@ instance Show Boolean where
show false = "false"
```

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_.

We can try out the `Show` type class in PSCi, by showing a few values with different types:
This code declares a type class instance; we say that the `Boolean` type _belongs to the `Show` type class_.

> If you're wondering, the generated JS code looks like this:
>
> ```javascript
> var showBoolean = {
> show: function (v) {
> if (v) {
> return "true";
> };
> if (!v) {
> return "false";
> };
> throw new Error("Failed pattern match at ...");
> }
> };
> ```
>
> If you're unhappy with the generated name, you can give names to type class instances. For example:
>
> ```haskell
> instance myShowBoolean :: Show Boolean where
> show true = "true"
> show false = "false"
> ```
>
> ```javascript
> var myShowBoolean = {
> show: function (v) {
> if (v) {
> return "true";
> };
> if (!v) {
> return "false";
> };
> throw new Error("Failed pattern match at ...");
> }
> };
> ```
We can try out the `Show` type class in PSCi by showing a few values with different types:
```text
> import Prelude
Expand Down

0 comments on commit 44013c6

Please sign in to comment.