@@ -5,7 +5,10 @@ module Data.Show
5
5
, showRecordFields
6
6
) where
7
7
8
+ import Data.Semigroup ((<>))
8
9
import Data.Symbol (class IsSymbol , reflectSymbol )
10
+ import Data.Unit (Unit )
11
+ import Data.Void (Void , absurd )
9
12
import Prim.Row (class Nub )
10
13
import Prim.RowList as RL
11
14
import Record.Unsafe (unsafeGet )
@@ -20,6 +23,9 @@ import Type.Proxy (Proxy(..))
20
23
class Show a where
21
24
show :: a -> String
22
25
26
+ instance showUnit :: Show Unit where
27
+ show _ = " unit"
28
+
23
29
instance showBoolean :: Show Boolean where
24
30
show true = " true"
25
31
show false = " false"
@@ -42,32 +48,43 @@ instance showArray :: Show a => Show (Array a) where
42
48
instance showProxy :: Show (Proxy a ) where
43
49
show _ = " Proxy"
44
50
51
+ instance showVoid :: Show Void where
52
+ show = absurd
53
+
45
54
instance showRecord ::
46
55
( Nub rs rs
47
56
, RL.RowToList rs ls
48
57
, ShowRecordFields ls rs
49
58
) =>
50
59
Show (Record rs ) where
51
- show record = case showRecordFields (Proxy :: Proxy ls ) record of
52
- [] -> " {}"
53
- fields -> intercalate " " [ " {" , intercalate " , " fields, " }" ]
60
+ show record = " {" <> showRecordFields (Proxy :: Proxy ls ) record <> " }"
54
61
55
62
-- | A class for records where all fields have `Show` instances, used to
56
63
-- | implement the `Show` instance for records.
57
64
class ShowRecordFields :: RL.RowList Type -> Row Type -> Constraint
58
65
class ShowRecordFields rowlist row where
59
- showRecordFields :: Proxy rowlist -> Record row -> Array String
66
+ showRecordFields :: Proxy rowlist -> Record row -> String
60
67
61
68
instance showRecordFieldsNil :: ShowRecordFields RL.Nil row where
62
- showRecordFields _ _ = []
63
-
69
+ showRecordFields _ _ = " "
70
+ else
71
+ instance showRecordFieldsConsNil ::
72
+ ( IsSymbol key
73
+ , Show focus
74
+ ) =>
75
+ ShowRecordFields (RL.Cons key focus RL.Nil ) row where
76
+ showRecordFields _ record = " " <> key <> " : " <> show focus <> " "
77
+ where
78
+ key = reflectSymbol (Proxy :: Proxy key )
79
+ focus = unsafeGet key record :: focus
80
+ else
64
81
instance showRecordFieldsCons ::
65
82
( IsSymbol key
66
83
, ShowRecordFields rowlistTail row
67
84
, Show focus
68
85
) =>
69
86
ShowRecordFields (RL.Cons key focus rowlistTail ) row where
70
- showRecordFields _ record = cons (intercalate " : " [ key, show focus ]) tail
87
+ showRecordFields _ record = " " <> key <> " : " <> show focus <> " , " <> tail
71
88
where
72
89
key = reflectSymbol (Proxy :: Proxy key )
73
90
focus = unsafeGet key record :: focus
@@ -78,5 +95,3 @@ foreign import showNumberImpl :: Number -> String
78
95
foreign import showCharImpl :: Char -> String
79
96
foreign import showStringImpl :: String -> String
80
97
foreign import showArrayImpl :: forall a . (a -> String ) -> Array a -> String
81
- foreign import cons :: forall a . a -> Array a -> Array a
82
- foreign import intercalate :: String -> Array String -> String
0 commit comments