@@ -5,7 +5,10 @@ module Data.Show
55 , showRecordFields
66 ) where
77
8+ import Data.Semigroup ((<>))
89import Data.Symbol (class IsSymbol , reflectSymbol )
10+ import Data.Unit (Unit )
11+ import Data.Void (Void , absurd )
912import Prim.Row (class Nub )
1013import Prim.RowList as RL
1114import Record.Unsafe (unsafeGet )
@@ -20,6 +23,9 @@ import Type.Proxy (Proxy(..))
2023class Show a where
2124 show :: a -> String
2225
26+ instance showUnit :: Show Unit where
27+ show _ = " unit"
28+
2329instance showBoolean :: Show Boolean where
2430 show true = " true"
2531 show false = " false"
@@ -42,32 +48,43 @@ instance showArray :: Show a => Show (Array a) where
4248instance showProxy :: Show (Proxy a ) where
4349 show _ = " Proxy"
4450
51+ instance showVoid :: Show Void where
52+ show = absurd
53+
4554instance showRecord ::
4655 ( Nub rs rs
4756 , RL.RowToList rs ls
4857 , ShowRecordFields ls rs
4958 ) =>
5059 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 <> " }"
5461
5562-- | A class for records where all fields have `Show` instances, used to
5663-- | implement the `Show` instance for records.
5764class ShowRecordFields :: RL.RowList Type -> Row Type -> Constraint
5865class ShowRecordFields rowlist row where
59- showRecordFields :: Proxy rowlist -> Record row -> Array String
66+ showRecordFields :: Proxy rowlist -> Record row -> String
6067
6168instance 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
6481instance showRecordFieldsCons ::
6582 ( IsSymbol key
6683 , ShowRecordFields rowlistTail row
6784 , Show focus
6885 ) =>
6986 ShowRecordFields (RL.Cons key focus rowlistTail ) row where
70- showRecordFields _ record = cons (intercalate " : " [ key, show focus ]) tail
87+ showRecordFields _ record = " " <> key <> " : " <> show focus <> " , " <> tail
7188 where
7289 key = reflectSymbol (Proxy :: Proxy key )
7390 focus = unsafeGet key record :: focus
@@ -78,5 +95,3 @@ foreign import showNumberImpl :: Number -> String
7895foreign import showCharImpl :: Char -> String
7996foreign import showStringImpl :: String -> String
8097foreign 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