From f1bd1ef5e7fedad332856b31ce5e998be0fc0f42 Mon Sep 17 00:00:00 2001 From: Zelenya Date: Wed, 29 Nov 2023 12:06:08 -0800 Subject: [PATCH] Showcase generated instances --- text/chapter6.md | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/text/chapter6.md b/text/chapter6.md index 01a36a3c..e3818a8d 100644 --- a/text/chapter6.md +++ b/text/chapter6.md @@ -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