Exit in Standard ML
Here’s an idiomatic Standard ML example that demonstrates the concept of program exit:
(* Exit.sml *)
(* Import the OS structure for exit functionality *)
structure OS = OS;
(* Define a function that will not be called *)
fun printMessage () = print "This will not be printed!\n";
(* Main program *)
val _ = (
(* Register a cleanup function that won't be called *)
OS.Process.atExit printMessage;
(* Exit the program with status code 3 *)
OS.Process.exit 3
);This Standard ML program demonstrates how to exit a program with a specific status code. Let’s break down the code and its concepts:
We import the
OSstructure, which provides system-level operations, including process management.We define a function
printMessagethat, if called, would print a message to the console.The main program is represented by the
val _ = (...)expression. In Standard ML, the underscore_is used when we don’t need to bind the result to a name.We use
OS.Process.atExit printMessageto register ourprintMessagefunction to be called when the program exits normally. However, this function will not be called when we useOS.Process.exit.Finally, we call
OS.Process.exit 3to immediately terminate the program with an exit status of 3.
To compile and run this program:
- Save the code in a file named
Exit.sml. - Use an Standard ML compiler like MLton to compile the program:
$ mlton Exit.smlThis will create an executable file named Exit.
- Run the program:
$ ./Exit
$ echo $?
3You’ll notice that no output is produced, and the exit status is 3.
Important notes:
- Unlike some other languages, Standard ML doesn’t use the return value of a
mainfunction to determine the exit status. Instead, we explicitly callOS.Process.exitwith the desired status code. - The
atExitfunction registers cleanup actions for normal program termination. However, when usingOS.Process.exit, these cleanup actions are not performed. - Standard ML doesn’t have a direct equivalent to Go’s
deferkeyword. TheatExitfunction provides similar functionality for program-wide cleanup, but it doesn’t work on a per-function basis like Go’sdefer.
This example demonstrates how to control program exit in Standard ML, which is a crucial concept in systems programming and error handling.