From 44013c65dcef31416b6986df1847c9b33afd3dac Mon Sep 17 00:00:00 2001 From: Zelenya Date: Thu, 30 Nov 2023 06:10:08 +0100 Subject: [PATCH] [Chapter 6] Add a note on optional type class instance names (#461) --- text/chapter6.md | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/text/chapter6.md b/text/chapter6.md index 30737052b..e3818a8d2 100644 --- a/text/chapter6.md +++ b/text/chapter6.md @@ -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