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 BAA18278; Wed, 19 Feb 2003 01:23:55 +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 BAA18164 for ; Wed, 19 Feb 2003 01:23:54 +0100 (MET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h1J0NqT11750 for ; Wed, 19 Feb 2003 01:23:53 +0100 (MET) Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3/3.7W) with ESMTP id JAA14815; Wed, 19 Feb 2003 09:23:42 +0900 (JST) To: malekith@pld-linux.org Cc: inv2002@yandex.ru, caml-list@inria.fr Subject: Re: [Caml-list] object In-Reply-To: <20030217172225.GB12324@roke.freak> References: <3E50A804.000001.28592@camay.yandex.ru> <20030217172225.GB12324@roke.freak> X-Mailer: Mew version 1.94.2 on Emacs 21.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20030219092342L.garrigue@kurims.kyoto-u.ac.jp> Date: Wed, 19 Feb 2003 09:23:42 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: Michal Moskal > On Mon, Feb 17, 2003 at 12:14:44PM +0300, inv2002 wrote: > > class a_obj = ... > > class b_obj = ... > > > > class object_list = object > > val ht = Hashtbl.create 5 > > > > method add: 'a. string -> (#obj as 'a) -> unit = fun key o -> > > Hashtbl.add ht key (o :> obj) (* i don't know right way, how to store objects *) > > > > method find key = > > Hashtbl.find ht key > > > > end > > > > i want to store a_obj and b_obj in object_list, > > after find it and execute a_fun method > > if the object is a_obj > > How do you know object is really of type a_obj? > > There are few solutions: > > 1. type a_or_b_obj = A_obj of a_obj | B_obj of b_obj > > method add k o = Hashtbl.add ht k o > > foo#add (A_obj a) > foo#add (B_obj b) > > 2. add a_fun and b_fun with ,,assert false'' like implementation > 3. use Obj.magic > > First solution gives you static typing, second gives you dynamic typing, > and third -- no typing. Please, never suggest using Obj.magic on this list. (Maybe we should have a special filter so that messages containing the words Obj.magic are reviewed by hand) Using Obj.magic in a user program is throwing away all the safety ensured by caml, going back to the C level. And gdb will not help you. If you look at the caml list archives with the keyword "downcast" you will see a whole litterature about how to solve this kind of problems http://pauillac.inria.fr/bin/wilma_glimpse/caml-list?query=downcast Solution (1) and (2) above are good enough for simple cases. If you don't like the restrictions caused by solution (1), my prefered answer uses parametric classes: class virtual ['a] obj = object method virtual real : 'a ... end class ['a] a_obj = object (self) inherit ['a] obj method real = `Ta self method a_fun = ... end class ['a] b_obj = object inherit ['a] obj method real = `Tb self method b_fun = ... end This way you can get back your original type by pattern matching on o#real. OO purists would say that it does not work well with subtyping, but if your problem is about subtyping then solution (2) above might be better. Note also that it may just be that your example does not need objects at all. This kind of by-case reasonning is often better handled by simple sum types. Cheers, Jacques ------------------- 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