next up previous contents
Next: Example Up: Foreign function interface (FFI) Previous: Foreign function interface (FFI)   Contents


Compile-time constants

MLton's _prim facility provides access to compile-time constants, which can be defined either via C include (.h) files or on the command line with the -D command-line option. The facility supports constants of type bool, int, real, string, and word. For example, the basis library implementation contains the following lines.

type syserror = int
val acces = _prim "Posix_Error_acces": syserror;
This defines the SML variable acces to be an int whose value is the value of the C constant (macro) Posix_Error_access, which is obtained from the (automatically) included file posix-constants.h. At compile-time, MLton generates a C file that prints the values of all _prim constants, calls gcc to produce an executable, runs the executable, and reads the result. The _prim expressions are then replaced by appropriate constants, which are available to the rest of the compilation process.