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 WAA23783 for caml-red; Wed, 15 Nov 2000 22:38:14 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id PAA20314 for ; Tue, 14 Nov 2000 15:41:32 +0100 (MET) Received: from beaune.inria.fr (beaune.inria.fr [128.93.8.3]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id eAEEfW901461 for ; Tue, 14 Nov 2000 15:41:32 +0100 (MET) Received: by beaune.inria.fr (8.8.8/1.1.22.3/14Sep99-0328PM) id PAA0000012997; Tue, 14 Nov 2000 15:41:32 +0100 (MET) Date: Tue, 14 Nov 2000 15:41:32 +0100 (MET) From: Damien Doligez Message-Id: <200011141441.PAA0000012997@beaune.inria.fr> To: caml-list@inria.fr Subject: Re: Is this use of Object.magic safe? Sender: weis@pauillac.inria.fr >From: Stephan Houben > >let value_of_int (i : int) : value = > Obj.magic i > >let value_of_allocated_value (av :allocated_value) : value= > Obj.magic av > >let unwrap (int_func : int -> 'a) > (aval_func : allocated_value -> 'a) > (v : value) : 'a = > if Obj.is_int (Obj.repr v) > then int_func (Obj.magic v) > else aval_func (Obj.magic v) >1. Is this safe w.r.t. the garbage collector? Or am I going to confuse the GC? No problem with the GC. >2. Can I safely marshall values? No problem with marshalling either. >3. Is it worth the trouble? Perhaps O'Caml already does something smart > when a constructor of the form `Int of int' is encountered? It doesn't work as you expect because O'Caml is strongly typed, so it can (and does) use a smart representation for your constant constructors True and False They are represented as integers, so your unwrap function cannot tell the difference between a boolean and an integer. This is typical of the dangers of using Obj.magic. -- Damien