Skip to content

Commit aac134b

Browse files
committed
Derive more instances for NullOrUndefined et. al
1 parent e88dae6 commit aac134b

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/Data/Foreign/Null.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Data.Foreign.Null where
33
import Prelude
44

55
import Data.Maybe (Maybe(..))
6+
import Data.Newtype (class Newtype, unwrap)
67
import Data.Foreign (F, Foreign, isNull)
78

89
-- | A `newtype` wrapper whose `IsForeign` instance correctly handles
@@ -12,6 +13,13 @@ import Data.Foreign (F, Foreign, isNull)
1213
-- | but not `undefined`.
1314
newtype Null a = Null (Maybe a)
1415

16+
derive instance newtypeNull :: Newtype (Null a) _
17+
derive instance eqNull :: (Eq a) => Eq (Null a)
18+
derive instance ordNull :: (Ord a) => Ord (Null a)
19+
20+
instance showNull :: (Show a) => Show (Null a) where
21+
show x = "Null " <> show (unwrap x)
22+
1523
-- | Unwrap a `Null` value
1624
unNull :: forall a. Null a -> Maybe a
1725
unNull (Null m) = m

src/Data/Foreign/NullOrUndefined.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module Data.Foreign.NullOrUndefined where
22

33
import Prelude
44

5+
import Data.Function (on)
6+
import Data.Newtype (class Newtype, unwrap)
57
import Data.Maybe (Maybe(..))
68
import Data.Foreign (F, Foreign, isUndefined, isNull)
79

@@ -12,6 +14,13 @@ import Data.Foreign (F, Foreign, isUndefined, isNull)
1214
-- | or `undefined`.
1315
newtype NullOrUndefined a = NullOrUndefined (Maybe a)
1416

17+
derive instance newtypeNullOrUndefined :: Newtype (NullOrUndefined a) _
18+
derive instance eqNullOrUndefined :: (Eq a) => Eq (NullOrUndefined a)
19+
derive instance ordNullOrUndefined :: (Ord a) => Ord (NullOrUndefined a)
20+
21+
instance showNullOrUndefined :: (Show a) => Show (NullOrUndefined a) where
22+
show x = "NullOrUndefined " <> show (unwrap x)
23+
1524
-- | Unwrap a `NullOrUndefined` value
1625
unNullOrUndefined :: forall a. NullOrUndefined a -> Maybe a
1726
unNullOrUndefined (NullOrUndefined m) = m

src/Data/Foreign/Undefined.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Data.Foreign.Undefined where
33
import Prelude
44

55
import Data.Maybe (Maybe(..))
6+
import Data.Newtype (class Newtype, unwrap)
67
import Data.Foreign (F, Foreign, isUndefined)
78

89
-- | A `newtype` wrapper whose `IsForeign` instance correctly handles
@@ -12,6 +13,13 @@ import Data.Foreign (F, Foreign, isUndefined)
1213
-- | but not `null`.
1314
newtype Undefined a = Undefined (Maybe a)
1415

16+
derive instance newtypeUndefined :: Newtype (Undefined a) _
17+
derive instance eqUndefined :: (Eq a) => Eq (Undefined a)
18+
derive instance ordUndefined :: (Ord a) => Ord (Undefined a)
19+
20+
instance showUndefined :: (Show a) => Show (Undefined a) where
21+
show x = "Undefined " <> show (unwrap x)
22+
1523
-- | Unwrap an `Undefined` value
1624
unUndefined :: forall a. Undefined a -> Maybe a
1725
unUndefined (Undefined m) = m

0 commit comments

Comments
 (0)