Skip to content

Commit 325f4c9

Browse files
committed
Merge pull request #2 from purescript/crash
Add crash and crashWith
2 parents ffaf7f3 + 7b58e05 commit 325f4c9

File tree

6 files changed

+60
-6
lines changed

6 files changed

+60
-6
lines changed

docs/Partial.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Module Partial
2+
3+
Some partial helper functions.
4+
5+
#### `crash`
6+
7+
``` purescript
8+
crash :: forall a. (Partial) => a
9+
```
10+
11+
A partial function which crashes on any input with a default message.
12+
13+
#### `crashWith`
14+
15+
``` purescript
16+
crashWith :: forall a. (Partial) => String -> a
17+
```
18+
19+
A partial function which crashes on any input with the specified message.
20+
21+

docs/Partial/Unsafe.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@ unsafePartial :: forall a. (Partial => a) -> a
1010

1111
Discharge a partiality constraint, unsafely.
1212

13+
#### `unsafeCrashWith`
14+
15+
``` purescript
16+
unsafeCrashWith :: forall a. String -> a
17+
```
18+
19+
A function which crashes with the specified error message.
20+
1321

src/Partial.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
// module Partial
4+
5+
exports.crashWith = function() {
6+
return function(msg) {
7+
throw new Error(msg);
8+
};
9+
};

src/Partial.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- | Some partial helper functions.
2+
module Partial
3+
( crash
4+
, crashWith
5+
) where
6+
7+
-- | A partial function which crashes on any input with a default message.
8+
crash :: forall a. Partial => a
9+
crash = crashWith "Partial.crash: partial function"
10+
11+
-- | A partial function which crashes on any input with the specified message.
12+
foreign import crashWith :: forall a. Partial => String -> a

src/Partial/Unsafe.purs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
-- | Utilities for working with partial functions.
22
module Partial.Unsafe
33
( unsafePartial
4+
, unsafeCrashWith
45
) where
56

67
-- | Discharge a partiality constraint, unsafely.
78
foreign import unsafePartial :: forall a. (Partial => a) -> a
9+
10+
-- | A function which crashes with the specified error message.
11+
unsafeCrashWith :: forall a. String -> a
12+
unsafeCrashWith msg = unsafePartial (Partial.crashWith msg)

test/Main.purs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
module Test.Main where
22

3-
data X = X | Y
3+
f :: Partial => Int -> Int
4+
f 0 = 0
5+
f _ = Partial.crashWith "f: partial function"
46

5-
f :: Partial => X -> X
6-
f X = X
7-
8-
safely :: X
9-
safely = Partial.Unsafe.unsafePartial (f X)
7+
safely :: Int
8+
safely = Partial.Unsafe.unsafePartial (f 0)
109

1110
main :: forall a. a -> {}
1211
main _ = {}

0 commit comments

Comments
 (0)