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 FAA24014; Sat, 31 Aug 2002 05:25:53 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 FAA24203 for ; Sat, 31 Aug 2002 05:25:52 +0200 (MET DST) Received: from mail3.tpgi.com.au (mail.tpgi.com.au [203.12.160.59]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g7V3PpD04887 for ; Sat, 31 Aug 2002 05:25:51 +0200 (MET DST) Received: from ozemail.com.au (syd-ts15-2600-232.tpgi.com.au [203.213.83.232]) (authenticated (0 bits)) by mail3.tpgi.com.au (8.11.6/8.11.6) with ESMTP id g7V3MrL13007; Sat, 31 Aug 2002 13:22:53 +1000 Message-ID: <3D703737.5030508@ozemail.com.au> Date: Sat, 31 Aug 2002 13:25:43 +1000 From: John Max Skaller User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.2.1) Gecko/20010901 X-Accept-Language: en-us MIME-Version: 1.0 To: David Frese CC: Caml-list Subject: Re: [Caml-list] Recursive type and class References: <1030710136.1831.11.camel@pc-6> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk David Frese wrote: > Hello folks, > > I have a problem with mixing a class and a type recursively. Written > down directly, I want to have something like this: > > type atype = Obj of aclass | I of int > > class aclass = object > val mutable field0 : atype = I 5 > end > > But this does not work, of course. Can anyone tell me, how this can be > done? You first declare a class *type* with a type parameter: class type ['t] aclass_t' = object val mutable field0: 't end Now you declare the recursion: type atype = Obj of atype aclass_t' | I of int and finally you make a typedef for the class: type aclass_t = atype aclass Well that's got the typing right, so now you can provide an implementation: class ['t] aclass init = object val mutable field0: atype = init end Note that the init value had to be lifted out, so you need a monomorphic function to instantiate the instance class: let mkclass () = new aclass (I 5) Finally, to check it works: # Obj (mkclass ());; - : atype = Obj [You might be able to make this simpler using a constraint, I've never quite figured them out :] -- John Max Skaller, mailto:skaller@ozemail.com.au snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia. voice:61-2-9660-0850 ------------------- 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