From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA22208 for caml-red; Wed, 24 May 2000 10:05:40 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA03155 for ; Tue, 23 May 2000 00:29:58 +0200 (MET DST) Received: from emissary.ot.com.au (emissary.ot.com.au [203.42.249.64]) by concorde.inria.fr (8.10.0/8.10.0) with SMTP id e4MMTuD18906 for ; Tue, 23 May 2000 00:29:56 +0200 (MET DST) Received: (qmail 1240 invoked from network); 22 May 2000 22:29:52 -0000 Received: from unknown (HELO nobel.in.ot.com.au) (10.70.10.5) by 10.70.15.1 with SMTP; 22 May 2000 22:29:52 -0000 Received: (qmail 17844 invoked from network); 22 May 2000 22:29:52 -0000 Received: from unknown (HELO in.ot.com.au) (maxs@10.70.82.63) by nobel.syd.ot with SMTP; 22 May 2000 22:29:52 -0000 Sender: weis Message-ID: <3929B4E0.30E6A7B1@in.ot.com.au> Date: Tue, 23 May 2000 08:29:52 +1000 From: Max Skaller X-Mailer: Mozilla 4.61 [en] (X11; I; Linux 2.2.12-20 i686) X-Accept-Language: en MIME-Version: 1.0 To: Pierre Weis CC: caml-list@inria.fr Subject: Re: reference initialization References: <200005221528.RAA04339@pauillac.inria.fr> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Pierre Weis wrote: > So, adding a test to detect this case we can initialize the vector > properly, without using Obj.magic. > > exception Not_yet_initialized of int;; > exception Already_initialized of int;; > exception Never_initialized of int;; > > let initialize n f = > if n = 0 then [||] else > let init_v = Array.make n false in > let v = ref [||] in > let get i = if init_v.(i) then !v.(i) else raise (Not_yet_initialized i) in > let set i ei = > if !v = [||] then v := Array.make n ei; Hmmm. This should work, even if 'ei' has a finaliser or mutable field: 'ei' isn't a 'dummy' value, but a real value that the client wanted in the array. On the other hand, a dummy value the client is forced to supply may have dire consequences where the type is either a class instance , or finalised: here either construction or destruction may have arbitrary semantics. So this (the code you gave) is much better than having to supply a dummy value. The same problem occurs with 'forced' variable initialisation: dummy values may have unwanted side-effects. There is a tension here, since ocaml is not a purely functional language. -- John (Max) Skaller at OTT [Open Telecommications Ltd] mailto:maxs@in.ot.com.au -- at work mailto:skaller@maxtal.com.au -- at home