1
1
module Data.Undefined.NoProblem where
2
2
3
3
import Prelude
4
-
5
- import Data.Either (Either )
6
4
import Data.Eq (class Eq1 , eq1 )
7
5
import Data.Maybe (Maybe (..), maybe )
8
- import Data.Tuple (Tuple )
9
- import Effect (Effect )
10
6
import Foreign (Foreign )
11
7
import Foreign (isUndefined ) as Foreign
12
- import Prim.RowList (class RowToList , Cons , Nil , kind RowList )
13
- import Prim.TypeError (class Fail , Above , Beside , Quote , QuoteLabel , Text , kind Doc )
8
+ import Prim.TypeError (Above , Beside , Quote , QuoteLabel , Text , kind Doc )
14
9
import Unsafe.Coerce (unsafeCoerce )
15
10
16
11
foreign import data Opt ∷ Type → Type
12
+
17
13
instance eqOpt ∷ Eq a ⇒ Eq (Opt a ) where
18
14
eq u1 u2 = toMaybe u1 == toMaybe u2
15
+
19
16
instance eq1Opt ∷ Eq a ⇒ Eq1 Opt where
20
17
eq1 u1 u2 = eq1 (toMaybe u1) (toMaybe u2)
18
+
21
19
instance ordOpt ∷ Ord a ⇒ Ord (Opt a ) where
22
20
compare u1 u2 = toMaybe u1 `compare` toMaybe u2
21
+
23
22
instance showOpt ∷ Show a ⇒ Show (Opt a ) where
24
23
show = maybe " undefined" (" Opt " <> _) <<< map show <<< toMaybe
25
24
@@ -33,16 +32,20 @@ fromOpt ∷ ∀ a. a → Opt a → a
33
32
fromOpt = flip fromOptFlipped
34
33
35
34
fromOptFlipped ∷ ∀ a . Opt a → a → a
36
- fromOptFlipped o default = if isUndefined o
37
- then default
38
- else unsafeCoerce o
35
+ fromOptFlipped o default =
36
+ if isUndefined o then
37
+ default
38
+ else
39
+ unsafeCoerce o
39
40
40
41
infixl 9 fromOptFlipped as !
41
42
42
43
toMaybe ∷ ∀ a . Opt a → Maybe a
43
- toMaybe o = if isUndefined o
44
- then Nothing
45
- else Just (unsafeUnwrap o)
44
+ toMaybe o =
45
+ if isUndefined o then
46
+ Nothing
47
+ else
48
+ Just (unsafeUnwrap o)
46
49
47
50
isUndefined ∷ ∀ a . Opt a → Boolean
48
51
isUndefined undef = Foreign .isUndefined (unsafeCoerce undef ∷ Foreign )
@@ -55,45 +58,50 @@ unsafeUnwrap = unsafeCoerce
55
58
-- |
56
59
-- | It is only to provide nice operator:
57
60
-- | (coerce {}) ? _.a ? _.b ? _.c.d ! "default"
58
- pseudoBind :: forall a b . Opt a -> (a -> Opt b ) -> Opt b
59
- pseudoBind o f = if isUndefined o
60
- then undefined
61
- else f (unsafeUnwrap o)
61
+ pseudoBind :: forall a b . Opt a -> (a -> Opt b ) -> Opt b
62
+ pseudoBind o f =
63
+ if isUndefined o then
64
+ undefined
65
+ else
66
+ f (unsafeUnwrap o)
62
67
63
68
infixl 9 pseudoBind as ?
64
69
65
70
-- | Ripped from typelevel-eval
66
71
infixr 2 type Beside as <>
72
+
67
73
infixr 1 type Above as |>
68
74
69
75
-- | Ripped from record-extra
70
76
foreign import kind SList
77
+
71
78
foreign import data SCons ∷ Symbol → SList → SList
79
+
72
80
foreign import data SNil ∷ SList
73
81
74
82
infixr 6 type SCons as :::
75
83
76
84
class RenderPath (path ∷ SList ) (render ∷ Doc ) | path → render
85
+
77
86
instance renderPathEnd ∷ RenderPath SNil (Text " " )
78
87
else instance renderPathLast ∷ RenderPath (n ::: SNil ) (QuoteLabel n )
79
- else instance renderPathSegment
80
- ∷ (RenderPath tail p )
81
- ⇒ RenderPath (segment ::: tail ) (p <> Text " ." <> QuoteLabel segment )
88
+ else instance renderPathSegment ∷
89
+ (RenderPath tail p ) ⇒
90
+ RenderPath (segment ::: tail ) (p <> Text " ." <> QuoteLabel segment )
82
91
83
92
class TypeMismatchErr given expected (path ∷ SList ) (msg ∷ Doc ) | path expected given → msg
84
- instance typeMismatchErr
85
- ∷ (RenderPath p p' )
86
- ⇒ TypeMismatchErr given expected p
87
- ( Text " Type mismatch on the path: { " <> p' <> Text " }. Expecting"
88
- |> Text " "
89
- |> Quote expected
90
- |> Text " "
91
- |> Text " but got"
92
- |> Text " "
93
- |> Quote given
94
- |> Text " "
95
- |> Text " If one of the types above is a type variable like `t2` or `r172`"
96
- |> Text " it probably means that you should provide type annotation to some"
97
- |> Text " parts of your value (like `[] ∷ Array Int` or `Nothing ∷ Maybe String`)"
98
- )
99
93
94
+ instance typeMismatchErr ∷
95
+ (RenderPath p p' ) ⇒
96
+ TypeMismatchErr given expected p ( Text " Type mismatch on the path: { " <> p' <> Text " }. Expecting"
97
+ |> Text " "
98
+ |> Quote expected
99
+ |> Text " "
100
+ |> Text " but got"
101
+ |> Text " "
102
+ |> Quote given
103
+ |> Text " "
104
+ |> Text " If one of the types above is a type variable like `t2` or `r172`"
105
+ |> Text " it probably means that you should provide type annotation to some"
106
+ |> Text " parts of your value (like `[] ∷ Array Int` or `Nothing ∷ Maybe String`)"
107
+ )
0 commit comments