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 KAA11690; Mon, 7 Jan 2002 10:24:25 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA11789 for caml-list@pauillac.inria.fr; Mon, 7 Jan 2002 10:24:25 +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 OAA17223 for ; Sat, 5 Jan 2002 14:10:32 +0100 (MET) Received: from libcom.com ([208.167.88.73]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g05DAUD23248 for ; Sat, 5 Jan 2002 14:10:30 +0100 (MET) Received: (from jprevost@localhost) by libcom.com (8.11.6/8.11.6) id g05DFWB83205; Sat, 5 Jan 2002 08:15:32 -0500 (EST) (envelope-from jprevost@libcom.com) Date: Sat, 5 Jan 2002 08:15:32 -0500 From: John Prevost To: wrader@OCF.Berkeley.EDU Cc: caml-list@inria.fr Subject: Re: [Caml-list] A class that contains instances of itself? Message-ID: <20020105081532.C79198@libcom.com> References: <200201051245.g05Cjxp24150@war.OCF.Berkeley.EDU> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <200201051245.g05Cjxp24150@war.OCF.Berkeley.EDU> User-Agent: Mutt/1.3.22.1i Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sat, Jan 05, 2002 at 04:45:59AM -0800, wrader@OCF.Berkeley.EDU wrote: >class someClass = > object > var mutable anotherInstanceOfSomeClass ??????? > > method talk () = > print_string("Hello.\n") > > method setInstance x = > anotherInstanceOfSomeClass <- x > > method useInstance () = > if anotherInstanceOfSomeClass is set to a > usable value, then do > anotherInstanceOfSomeClass#talk > else > do nothing > end;; > > >One must call anotherInstanceOfSomeClass#setInstance >before using anotherInstanceOfSomeClass#talk. > >What do I put in place of the ?????? to indicate >that the value is not yet usable? I could create >a "dummy" someClass object to use as the "NULL" value, >let's say called someClassNULL. someClassNULL must >already be defined if I use it in the body of >someClass, but I can't create it before the >definition of someClass because the compiler doesn't >know what someClass _is_ yet. Catch-22!! > >I've been staring at this problem for hours, so any >help would be fantastic! (If only I spoke French and >could read the many books available about OCaml...) In this case, I would write: object ('s) val mutable anotherInstanceOfSomeClass = (ref None : 's option) method talk () = print_string("Hello.\n") method setInstance x = anotherInstanceOfSomeClass <- Some x method useInstance () = match anotherInstanceOfSomeClass | Some x -> anotherInstanceOfSomeClass#talk () | None -> () end The ('s) part binds the "self type" of the class so you can refer to it. "foo option" is either "None" or "Some x" where x is a value of type foo. The option type is the wonderful way that languages with reasonably powerful type systems allow us to add something like "NULL" only where we want it, instead of having it everywhere. Another way of looking at things (in this case) is that you only need some object with a talk method in that value--not necessarily the same type object as your class, so you could do: class doNothing = object method talk () = () end and then use an instance of that as a placeholder. But, this will lead to other typing issues, as you'll have to cast everything to only include the talk method before you pass them in. (Ignore this for now if it doesn't make sense to you, and read through all of the object examples in the manual.) Do read the O'Caml manual. It's written in English, and while it doesn't recommend ways of doing everything like this, actually reading through it from end to end can suggest a lot of ways to do things. John. ------------------- 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