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 RAA00451; Fri, 8 Feb 2002 17:53:34 +0100 (MET) 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 RAA00406 for ; Fri, 8 Feb 2002 17:53:34 +0100 (MET) Received: from mel-rto6.wanadoo.fr (smtp-out-6.wanadoo.fr [193.252.19.25]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g18GrXr10384 for ; Fri, 8 Feb 2002 17:53:33 +0100 (MET) Received: from mel-rta10.wanadoo.fr (193.252.19.193) by mel-rto6.wanadoo.fr; 8 Feb 2002 17:53:33 +0100 Received: from debian (80.8.74.6) by mel-rta10.wanadoo.fr; 8 Feb 2002 17:53:26 +0100 Received: from moi by debian with local (Exim 3.34 #1 (Debian)) id 16ZEHN-00007x-00 for ; Fri, 08 Feb 2002 17:53:41 +0100 To: caml-list@inria.fr Subject: Re: [Caml-list] Allowing many types References: <20020208163700.GA1452@mandelbrot.house> From: Remi VANICAT Date: 08 Feb 2002 17:53:41 +0100 In-Reply-To: <20020208163700.GA1452@mandelbrot.house> Message-ID: <87vgd7oqy2.dlv@wanadoo.fr> User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Ceri Storey writes: > I've been trying to do something similar with something i'm writing, and > the only thing i could come up with is to store data in an object, and > then subclass it. The problem here is that once the object has been > coerced to the general type, it cannot be coerced back again. ie: if you > subclass the object to say, impliment integers, then it's not possible > for it to be coerced back to the integer type so you can retreive / act > on the value. (withoug using Obj.magic, that is) > > If anyone has any good ideas, I'd be happy to hear about them too. polymorphic variant me be the answer, because you can get back the type : let fn1 (`Int x) = float_of_int x let fn2 (`Float x) = x let fn x = match x with | `Int _ as i -> fn1 i | `Float _ as f -> fn2 f | _ -> raise (Invalid_argument "fn") (be aware that let fn x = match x with | `Int _ as i -> fn1 x | `Float _ as f -> fn2 x | _ -> raise (Invalid_argument "fn") doesn't work) extensible function from camlp4 library may be also useful, but I've not try it yet. -- Rémi Vanicat vanicat@labri.u-bordeaux.fr http://dept-info.labri.u-bordeaux.fr/~vanicat ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr