Module Toolkit.Global


module Global: sig .. end
Modified Global module from Extlib library, Copyright (C) 2003 Nicolas Cannasse

Mutable global variable.

Often in OCaml you want to have a global variable, which is mutable and uninitialized when declared. You can use a 'a option ref but this is not very convenient. The Global module provides functions to easily create and manipulate such variables.


exception Global_not_initialized of string
Raised when a global variable is accessed without first having been assigned a value. The parameter contains the name of the global.
type 'a t = 'a option Pervasives.ref * string 
Abstract type of a global
val empty : 'a -> 'b option Pervasives.ref * 'a
Returns an new named empty global. The name of the global can be any string. It identifies the global and makes debugging easier.
val name : 'a * 'b -> 'b
Retrieve the name of a global.
val set : 'a option Pervasives.ref * 'b -> 'a -> unit
Set the global value contents.
val get : 'a option Pervasives.ref * string -> 'a
Get the global value contents - raise Global_not_initialized if not defined.
val getd : 'a -> 'a option Pervasives.ref * 'b -> 'a
Get the global value contents - return default value if not defined
val undef : 'a option Pervasives.ref * 'b -> unit
Reset the global value contents to undefined.
val isdef : 'a option Pervasives.ref * 'b -> bool
Return true if the global value has been set.
val opt : 'a Pervasives.ref * 'b -> 'a
Return None if the global is undefined, else Some v where v is the current global value contents.