Next: , Previous: Submodules, Up: Modules   [Contents]


9.4 Module initialisation

Modules that interact with foreign libraries or services may require special initialisation before use. Such modules may include any number of ‘initialise’ directives in their implementation sections. An ‘initialise’ directive has the following form:

:- initialise initpredname/arity.

where the predicate initpredname must be declared with one of the following signatures:

:- pred initpredname(io::di, io::uo) is Det.
:- impure pred initpredname is Det.

Det must be either det or cc_multi.

The effect of the ‘initialise’ declaration is to ensure that ‘initpredname/arity’ is invoked before the program’s ‘main/2’ predicate. Initialisation predicates within a module are executed in the order in which they are specified, although no order may be assumed between different modules or submodules. Initialisation predicates are only invoked after any initialisation required by the Mercury standard library.

If ‘initpredname/arity’ terminates with an uncaught exception then the program will immediately abort execution. In this circumstance, those predicates specified by other ‘initialise’ directives that have not yet been executed will not be executed, ‘main/2’ will not be executed, and no predicate specified in a ‘finalise’ directive will be executed.

initialize’ is also allowed as a synonym for ‘initialise’.