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 LAA16086; Mon, 5 Aug 2002 11:30:52 +0200 (MET DST) 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 LAA16124 for ; Mon, 5 Aug 2002 11:30:51 +0200 (MET DST) 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 g759UnD15738 for ; Mon, 5 Aug 2002 11:30:49 +0200 (MET DST) 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 SAA14216; Mon, 5 Aug 2002 18:30:43 +0900 (JST) To: shoh@duonix.com Cc: caml-list@inria.fr Subject: Re: [Caml-list] Weak type, Open type In-Reply-To: <000501c23c4f$2b781c50$fe00a8c0@hama> References: <000501c23c4f$2b781c50$fe00a8c0@hama> X-Mailer: Mew version 1.94.2 on Emacs 20.7 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20020805183043B.garrigue@kurims.kyoto-u.ac.jp> Date: Mon, 05 Aug 2002 18:30:43 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: "SooHyoung Oh" > While I've studying design pattern with ocaml, > I made the followding 3 classes (PART1): > (1) class virtual entry: base class > (2) class file: subclass of "entry" class > (3) class dir: subclass of "entry" class > > I made some code using these classes on top level and it's ok. (PART 2). > While I translate the top level code into new class definition (PART3), it > makes some error. > The error message is on (PART 4). > > It seems that (PART2) is weak type and (PART 3) is open type. Not weak: file_list has a weak type variable, but it gets immediatly instanciated to entry; and add is really polymorphic. > [Q 1] Is (PART2) is correct? I don't know why it doesn't make type error. Yes. What's wrong? Note that your #entry is meaningless, since it is immediatly instanciated to entry. You could as well write entry list from the beginning, or nothing at all. > [Q 2] (depend on Q 1) > Can I translate (PART2) into new class definition like (PART 3) without type > error? The problem is witht the polymorphism of add. Implicit polymorphism is not allowed in methods. You can either remove the polymorphism, or make it explicit. No polymorphism: class dir2 name = object(self) inherit entry () val file_list = ref ([]: entry list) method name () = name method add e = file_list := e :: !file_list end;; Explicit polymorphism (3.05 only): class dir2 name = object(self) inherit entry () val file_list = ref [] method name () = name method add : 'a. (#entry as 'a) -> unit = fun e -> file_list := (e:>entry) :: !file_list end;; Jacques Garrigue > ============ (PART 1) class definition =============== > class virtual entry () = > object > method virtual name: unit -> string > end;; > > class file name = > object(self) > inherit entry () > method name () = name > method file_size () = 100 > end;; > > class dir name = > object(self) > inherit entry () > method name () = name > method dir_size () = 3000 > end;; > > =============== (PART 2) NO ERROR with weak type (?) =================== > let file_list = ref ([]: #entry list);; > let add e = file_list := (e:>entry) :: !file_list;; > let _ = add (new dir "root");; > let _ = add (new file "hello.txt");; > > ================ (PART 3) ERROR with open type (?) ================ > class dir2 name = > object(self) > inherit entry () > > val file_list = ref ([]: #entry list) > > method name () = name > method add e = file_list := (e:>entry) :: !file_list > end;; > > ================ (PART 4) error message ================ > Some type variables are unbound in this type: > class dir2 : > string -> > object > val file_list : entry list ref > method add : #entry -> unit > method name : unit -> string > end > The method add has type #entry -> unit where .. is unbound > > --- > SooHyoung Oh > tel: 02)583-8709, 042)861-8649 > cell. phone: 011-453-4303 > web: http://www.duonix.com > > > ------------------- > 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 ------------------- 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