-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathGeneric.purs
58 lines (46 loc) · 1.29 KB
/
Generic.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Main where
import Prelude
import Effect (Effect)
import Effect.Console (logShow)
import Data.Generic.Rep (class Generic)
import Data.Eq.Generic (genericEq)
import Data.Ord.Generic (genericCompare)
import Data.Show.Generic (genericShow)
import TryPureScript (render, withConsole)
data Address = Address
{ city :: String
, state :: String
}
data Person = Person
{ first :: String
, last :: String
, address :: Address
}
-- Generic instances can be derived by the compiler,
-- using the derive keyword:
derive instance genericAddress :: Generic Address _
derive instance genericPerson :: Generic Person _
-- Now we can write instances for standard type classes
-- (Show, Eq, Ord) by using standard definitions
instance showAddress :: Show Address where
show = genericShow
instance eqAddress :: Eq Address where
eq = genericEq
instance ordAddress :: Ord Address where
compare = genericCompare
instance showPerson :: Show Person where
show = genericShow
instance eqPerson :: Eq Person where
eq = genericEq
instance ordPerson :: Ord Person where
compare = genericCompare
main :: Effect Unit
main = render =<< withConsole do
logShow $ Person
{ first: "John"
, last: "Smith"
, address: Address
{ city: "Faketown"
, state: "CA"
}
}