Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
one-time initialization
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1999-01-28 (12:21)
From: John Prevost <prevost@m...>
Subject: Re: one-time initialization
Michael Hicks <> writes:

> I wonder if anyone knows how to optimize the following (simplified for the
> sake of dicussion) situation:

The only alternative I can think of that might provide a significant
performance boost is to make the reference a "default" value when the
initialization hasn't yet been done.  Another alternative that may win
some speed (but I sort of doubt it) is to make the reference be to a
function that raises an exception, like so:

val global = (ref (fun () -> failwith "not initialized") :
	(mytype -> unit) ref)

let init i = global := fun () -> i

let f () = ... (!global ())
let g () = (!global ()) ...

this at least has the advantage that you don't have to have a big
hairy pattern match in every function.