Leaving implementation details for later with unsafeCrashWith in PureScript

Sometimes, while designing our code, a new idea pops up and we start thinking 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 function defined in the Partial.Unsafe module comes in handy.

Although, that's not what type safety encourages, it allows us to write function definitions such as this:

capitalize :: String -> String
capitalize = unsafeCrashWith "Not implemented yet"

Type checker does not complain. However, when called, this function would fail miserably:

    throw new Error(msg);
    ^

Error: Not implemented yet

Nevertheless, avoiding the 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 the program function is not monolithic anymore.

Once we're happy with the overall design of our code, all that is left is actually providing the implementation.