Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/Partial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Module Partial

Some partial helper functions.

#### `crash`

``` purescript
crash :: forall a. (Partial) => a
```

A partial function which crashes on any input with a default message.

#### `crashWith`

``` purescript
crashWith :: forall a. (Partial) => String -> a
```

A partial function which crashes on any input with the specified message.


8 changes: 8 additions & 0 deletions docs/Partial/Unsafe.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ unsafePartial :: forall a. (Partial => a) -> a

Discharge a partiality constraint, unsafely.

#### `unsafeCrashWith`

``` purescript
unsafeCrashWith :: forall a. String -> a
```

A function which crashes with the specified error message.


9 changes: 9 additions & 0 deletions src/Partial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

// module Partial

exports.crashWith = function() {
return function(msg) {
throw new Error(msg);
};
};
12 changes: 12 additions & 0 deletions src/Partial.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- | Some partial helper functions.
module Partial
( crash
, crashWith
) where

-- | A partial function which crashes on any input with a default message.
crash :: forall a. Partial => a
crash = crashWith "Partial.crash: partial function"

-- | A partial function which crashes on any input with the specified message.
foreign import crashWith :: forall a. Partial => String -> a
5 changes: 5 additions & 0 deletions src/Partial/Unsafe.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
-- | Utilities for working with partial functions.
module Partial.Unsafe
( unsafePartial
, unsafeCrashWith
) where

-- | Discharge a partiality constraint, unsafely.
foreign import unsafePartial :: forall a. (Partial => a) -> a

-- | A function which crashes with the specified error message.
unsafeCrashWith :: forall a. String -> a
unsafeCrashWith msg = unsafePartial (Partial.crashWith msg)
11 changes: 5 additions & 6 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module Test.Main where

data X = X | Y
f :: Partial => Int -> Int
f 0 = 0
f _ = Partial.crashWith "f: partial function"

f :: Partial => X -> X
f X = X

safely :: X
safely = Partial.Unsafe.unsafePartial (f X)
safely :: Int
safely = Partial.Unsafe.unsafePartial (f 0)

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