Exit in PureScript
Here’s an idiomatic PureScript example demonstrating the concept of exiting a program:
This PureScript program demonstrates how to exit a program with a specific status code. Let’s break it down:
We import necessary modules, including
Node.Process
for theexit
function.The
main
function is defined as anEffect Unit
, which is PureScript’s way of handling side effects.We use
log
to print a message to the console before exiting.The
exit
function fromNode.Process
is used to immediately terminate the program with a status code of 3.Any code after the
exit
call will not be executed, similar to theos.Exit
behavior in the original example.
To run this program:
- Save the code in a file named
Main.purs
. - Compile the code using the PureScript compiler:
- Run the compiled JavaScript using Node.js:
To check the exit status:
Note that the second log message is never printed, as the program exits before reaching that line.
In PureScript, unlike some other languages, the main
function doesn’t return an exit code. Instead, you need to explicitly call exit
to terminate the program with a non-zero status code.
This example showcases how PureScript, a purely functional language, handles program termination and side effects in a type-safe manner while still providing low-level control over process exit status.