From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA16797; Sat, 1 Jun 2002 19:44:33 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 TAA16887 for ; Sat, 1 Jun 2002 19:44:32 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g51HgY520514; Sat, 1 Jun 2002 19:42:45 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA16993; Sat, 1 Jun 2002 19:42:34 +0200 (MET DST) From: Pierre Weis Message-Id: <200206011742.TAA16993@pauillac.inria.fr> Subject: Re: [Caml-list] about Obj.magic In-Reply-To: <3CF7BF45.40201@ozemail.com.au> from John Max Skaller at "Jun 1, 102 04:21:57 am" To: skaller@ozemail.com.au (John Max Skaller) Date: Sat, 1 Jun 2002 19:42:34 +0200 (MET DST) Cc: warplayer@free.fr, garrigue@kurims.kyoto-u.ac.jp, caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > An 'almost legal' use of Obj.magic is to populate an array with dummy > values. > For example: > > class Deep .. > let a = Array.make 100 (Obj.magic 0 :> Deep) in > let len_a = ref 0 in > > This is roughly as safe as an unsafe array access, > only now the array length is a variable. > > -- > John Max Skaller, mailto:skaller@ozemail.com.au > snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia. > voice:61-2-9660-0850 I'm afraid that this use is not ``almost legal'' and, on the contrary, it is strongly discouraged by good programming practice! However, your example is interesting as a good counterexample: this is typical of a bad usage of Obj.magic! The program you gave is indeed wrong, unless you have a proof that all the elements of array ``a'' will be assigned before accessed, and may be also you have an additional proof that the memory management system of Caml can deal with the integer "0" as a valid object of the class Deep. Hence, this occurrence of Obj.magic is unsafe, error prone, and should not be encouraged. The rule of thumb for usages of Obj.magic is as Jacques already stated it: you need a theorem to write Obj.magic. That's the way the Caml system uses it: we always have a meta theorem that proves that this very occurence of Obj.magic is safe. Otherwise, the resulting program has always the same property : one day or another, it gives you Bus error. Thank you for your example that will help Caml users. Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners