Re: one-time initialization

From: John Prevost (prevost@maya.com)
Date: Thu Jan 28 1999 - 10:54:07 MET


To: caml-list@inria.fr
Subject: Re: one-time initialization
From: John Prevost <prevost@maya.com>
Date: 28 Jan 1999 04:54:07 -0500
In-Reply-To: Michael Hicks's message of "Thu, 28 Jan 1999 00:08:23 -0500 (EST)"

Michael Hicks <mwh@dsl.cis.upenn.edu> 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.

jmp



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:18 MET