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 PAA14858 for caml-red; Tue, 14 Nov 2000 15:17:09 +0100 (MET) 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 KAA11778 for ; Mon, 13 Nov 2000 10:00:45 +0100 (MET) Received: from pcrm.win.tue.nl (pcrm.win.tue.nl [131.155.69.41]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id eAD90ir25593 for ; Mon, 13 Nov 2000 10:00:44 +0100 (MET) Received: from pcrm (stephan@localhost [127.0.0.1]) by pcrm.win.tue.nl (8.9.3/8.9.3/SuSE Linux 8.9.3-0.1) with SMTP id KAA28685 for ; Mon, 13 Nov 2000 10:04:40 +0100 From: Stephan Houben Reply-To: stephanh@win.tue.nl Organization: Scientific Computing Group To: caml-list@inria.fr Subject: Is this use of Object.magic safe? Date: Mon, 13 Nov 2000 09:43:48 +0100 X-Mailer: KMail [version 1.0.28] Content-Type: text/plain MIME-Version: 1.0 Message-Id: <00111310044001.28230@pcrm> Content-Transfer-Encoding: 8bit Sender: weis@pauillac.inria.fr Hello list, I am currently writing a small interpreted language for my app. The language is dynamically typed, which means that a value is represented somewhat like this: type value = True | False | Int of int | String of string | Array of value array Now a well-known optimisation for such an interpreter is to make small int's not heap-allocated objects but to represent them directly in the pointer. In fact, O'Caml itself already does this. Moreover, it allows access to this info via the Obj module. My idea is to make "value" an abstract type, and have a separate type allocated_value, which doesn't contain the Int constructor: type value type allocated_value = True | False | String of string | Array of value Then I can use Obj.magic to make constructor and destructor functions for value: 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) I have three questions: 1. Is this safe w.r.t. the garbage collector? Or am I going to confuse the GC? 2. Can I safely marshall values? 3. Is it worth the trouble? Perhaps O'Caml already does something smart when a constructor of the form `Int of int' is encountered? Stephan -- ir. Stephan H.M.J. Houben tel. +31-40-2474358 / +31-40-2743497 e-mail: stephanh@win.tue.nl