Skip to content

Commit

Permalink
Showcase generated instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Zelenya committed Nov 29, 2023
1 parent dfa4a54 commit f1bd1ef
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions text/chapter6.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,45 @@ instance Show Boolean where

This code declares a type class instance; we say that the `Boolean` type _belongs to the `Show` type class_.

> Optionally, we can give names to type class instances to aid the readability of the generated JavaScript. For example:
> If you're wondering, the generated JS code looks like this:
>
> ```haskell
> instance showBoolean :: Show Boolean where
> show true = "true"
> show false = "false"
> ```
We can try out the `Show` type class in PSCi, by showing a few values with different types:
> ```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 f1bd1ef

Please sign in to comment.