File tree Expand file tree Collapse file tree 3 files changed +21
-1
lines changed Expand file tree Collapse file tree 3 files changed +21
-1
lines changed Original file line number Diff line number Diff line change 55exports . unsafePartial = function ( f ) {
66 return f ( ) ;
77} ;
8+
9+ exports . unsafePartialBecause = function ( reason ) {
10+ return function ( f ) {
11+ try {
12+ return exports . unsafePartial ( f ) ;
13+ } catch ( err ) {
14+ throw new Error ( "unsafePartial failed. The following " +
15+ "assumption was incorrect: '" + reason + "'." ) ;
16+ }
17+ } ;
18+ } ;
Original file line number Diff line number Diff line change 11-- | Utilities for working with partial functions.
22module Partial.Unsafe
33 ( unsafePartial
4+ , unsafePartialBecause
45 , unsafeCrashWith
56 ) where
67
@@ -9,6 +10,11 @@ import Partial (crashWith)
910-- | Discharge a partiality constraint, unsafely.
1011foreign import unsafePartial :: forall a . (Partial => a ) -> a
1112
13+ -- | Discharge a partiality constraint, unsafely. Raises an exception with a
14+ -- | custom error message in the (unexpected) case where `unsafePartial` was
15+ -- | used incorrectly.
16+ foreign import unsafePartialBecause :: forall a . String -> (Partial => a ) -> a
17+
1218-- | A function which crashes with the specified error message.
1319unsafeCrashWith :: forall a . String -> a
1420unsafeCrashWith msg = unsafePartial (crashWith msg)
Original file line number Diff line number Diff line change 11module Test.Main where
22
33import Partial (crashWith )
4- import Partial.Unsafe (unsafePartial )
4+ import Partial.Unsafe (unsafePartial , unsafePartialBecause )
55
66f :: Partial => Int -> Int
77f 0 = 0
@@ -10,5 +10,8 @@ f _ = crashWith "f: partial function"
1010safely :: Int
1111safely = unsafePartial (f 0 )
1212
13+ safely2 :: Int
14+ safely2 = unsafePartialBecause " calling f with argument 0 is guaranteed to be safe" (f 0 )
15+
1316main :: forall a . a -> { }
1417main _ = {}
You can’t perform that action at this time.
0 commit comments