diff --git a/docs/Partial.md b/docs/Partial.md new file mode 100644 index 0000000..8fd3a12 --- /dev/null +++ b/docs/Partial.md @@ -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. + + diff --git a/docs/Partial/Unsafe.md b/docs/Partial/Unsafe.md index 8e545f2..3c9a5eb 100644 --- a/docs/Partial/Unsafe.md +++ b/docs/Partial/Unsafe.md @@ -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. + diff --git a/src/Partial.js b/src/Partial.js new file mode 100644 index 0000000..bfac946 --- /dev/null +++ b/src/Partial.js @@ -0,0 +1,9 @@ +"use strict"; + +// module Partial + +exports.crashWith = function() { + return function(msg) { + throw new Error(msg); + }; +}; diff --git a/src/Partial.purs b/src/Partial.purs new file mode 100644 index 0000000..3943f4f --- /dev/null +++ b/src/Partial.purs @@ -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 diff --git a/src/Partial/Unsafe.purs b/src/Partial/Unsafe.purs index f38d592..037e375 100644 --- a/src/Partial/Unsafe.purs +++ b/src/Partial/Unsafe.purs @@ -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) diff --git a/test/Main.purs b/test/Main.purs index c6cbb0f..fb6524e 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -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 _ = {}