File tree Expand file tree Collapse file tree 3 files changed +26
-14
lines changed Expand file tree Collapse file tree 3 files changed +26
-14
lines changed Original file line number Diff line number Diff line change @@ -26,11 +26,12 @@ data PhoneType
26
26
| OtherPhone
27
27
28
28
-- ANCHOR: PhoneType_generic
29
- derive instance genericPhoneType :: Generic PhoneType _
30
- instance encodeJsonPhoneType :: EncodeJson PhoneType where encodeJson = genericEncodeJson
31
- instance decodeJsonPhoneType :: DecodeJson PhoneType where decodeJson = genericDecodeJson
29
+ derive instance genericPhoneType :: Generic PhoneType _
30
+
31
+ instance encodeJsonPhoneType :: EncodeJson PhoneType where encodeJson = genericEncodeJson
32
+ instance decodeJsonPhoneType :: DecodeJson PhoneType where decodeJson = genericDecodeJson
32
33
-- ANCHOR_END: PhoneType_generic
33
- instance showPhoneType :: Show PhoneType where show = genericShow
34
+ instance showPhoneType :: Show PhoneType where show = genericShow
34
35
35
36
type PhoneNumber
36
37
= { "type" :: PhoneType
Original file line number Diff line number Diff line change 1
1
module Data.AddressBook where
2
2
3
3
import Prelude
4
+ import Data.Generic.Rep (class Generic )
5
+ import Data.Generic.Rep.Show (genericShow )
4
6
5
7
type Address
6
8
= { street :: String
@@ -21,18 +23,26 @@ data PhoneType
21
23
| OtherPhone
22
24
-- ANCHOR_END: PhoneType
23
25
24
- {-| derive has not been discussed yet but will be
25
- covered in Ch 10. Here it is needed by the unit
26
- tests to define how to compare the PhoneType values
26
+ {-
27
+ Eq and Show instances are needed by unit tests to
28
+ compare and report differences between PhoneType values
27
29
(HomePhone, WorkPhone, etc).
28
30
-}
29
31
derive instance eqPhoneType :: Eq PhoneType
30
32
33
+ -- Generic Show instance
34
+ derive instance genericPhoneType :: Generic PhoneType _
35
+
36
+ instance showPhoneType :: Show PhoneType where
37
+ show = genericShow
38
+ {-
39
+ -- Manually-written Show instance
31
40
instance showPhoneType :: Show PhoneType where
32
- show HomePhone = " HomePhone"
33
- show WorkPhone = " WorkPhone"
34
- show CellPhone = " CellPhone"
41
+ show HomePhone = "HomePhone"
42
+ show WorkPhone = "WorkPhone"
43
+ show CellPhone = "CellPhone"
35
44
show OtherPhone = "OtherPhone"
45
+ -}
36
46
37
47
type PhoneNumber
38
48
= { "type" :: PhoneType
Original file line number Diff line number Diff line change 1
1
module Data.AddressBook where
2
2
3
3
import Prelude
4
+ import Data.Generic.Rep (class Generic )
5
+ import Data.Generic.Rep.Show (genericShow )
4
6
5
7
type Address
6
8
= { street :: String
@@ -17,11 +19,10 @@ data PhoneType
17
19
| CellPhone
18
20
| OtherPhone
19
21
22
+ derive instance genericPhoneType :: Generic PhoneType _
23
+
20
24
instance showPhoneType :: Show PhoneType where
21
- show HomePhone = " HomePhone"
22
- show WorkPhone = " WorkPhone"
23
- show CellPhone = " CellPhone"
24
- show OtherPhone = " OtherPhone"
25
+ show = genericShow
25
26
26
27
type PhoneNumber
27
28
= { "type" :: PhoneType
You can’t perform that action at this time.
0 commit comments