Leaving implementation details for later with unsafeCrashWith in PureScript
Sometimes, while designing our code, a new idea pops up and we start thinking about how well would this thing we currently work on compose with the other bits of our application.
Focusing on implementation details might be a waste of time if we just want to test an idea out.
unsafeCrashWith
In these rare cases, the unsafeCrashWith :: forall a. String -> a
defined in Partial.Unsafe module comes in handy.
Although that's not what type-safe code would look like, it allows us to write function definitions such as this:
capitalize :: String -> String
capitalize = unsafeCrashWith "Not implemented yet"
The type checker does not complain. However, when called, this function would fail miserably:
throw new Error(msg);
^
Error: Not implemented yet
Nevertheless, avoiding failure is not our point here. We don't want to call this function just yet.
Example
To make a practical example, imagine we had a monolithic function from string to string program :: String -> String
.
Could it be split into a composition of smaller functions? What would the types of these smaller functions be?
Let's see an example of turning program
into a composition of two functions, capitalize
and greet
with no actual definitions.
module Main where
import Prelude
import Partial.Unsafe (unsafeCrashWith)
capitalize :: String -> String
capitalize = unsafeCrashWith "Not implemented yet"
greet :: String -> String
greet = unsafeCrashWith "Not implemented yet"
program :: String -> String
program = greet <<< capitalize
This snippet actually compiles and program
is not a monolithic function anymore.
Once we're happy with the overall design of our code, all that is left is actually providing the implementation.