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 TAA22163; Fri, 23 Aug 2002 19:32:46 +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 TAA22228 for ; Fri, 23 Aug 2002 19:32:45 +0200 (MET DST) Received: from smtp2.mathworks.com (smtp.mathworks.com [144.212.95.12]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g7NHWiv07299 for ; Fri, 23 Aug 2002 19:32:44 +0200 (MET DST) Received: from mail.mathworks.com (turing.mathworks.com [144.212.95.101]) by smtp2.mathworks.com (8.11.6/8.11.6) with ESMTP id g7NHWgK07904; Fri, 23 Aug 2002 13:32:42 -0400 (EDT) Received: from smithf (smithf.dhcp.mathworks.com [144.212.115.83]) by mail.mathworks.com (8.11.2/8.11.2) with SMTP id g7NHWg320411; Fri, 23 Aug 2002 13:32:42 -0400 (EDT) From: "Fred Smith" To: "Frederic Tronel" , Subject: RE: [Caml-list] Polymorphic methods Date: Fri, 23 Aug 2002 13:32:42 -0400 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0) X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700 Importance: Normal In-Reply-To: <3D6658D0.F18A453D@inrialpes.fr> Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi, The method set_t would not be type safe given the type you have assigned it. OK, the code snippet you wrote happens to be safe but won't be as soon as you try to use it. With the type you have assigned set_t the following would be legal: n#set_t 3.4; n#set_t "hello"; because set_t takes any type as input ('a.'a->unit). What type would n#t have? Neither float option nor string option will work. In particular, if you had provided a function get_t it could not return a useful single type. To see the problem consider if myWhackoCondition then (n#set_t 3.4) else (n#set_t "hello"); let w = n#get_t () in (* What type should w have? *) A class definition that is legal and may solve your problem is: class ['a] node = object val mutable t : 'a option = None method set_t nt = t<- Some nt method get_t () = (match t with Some x -> x | _ -> failwith "Empty node") method is_empty () = (t = None) end Now you can create values of type int node, float node, etc... and set_t and get_t will return the appropriate types. But you cannot create a value of type 'a.'a node for the same reasons as above. Hope this helps. -Fred > -----Original Message----- > From: owner-caml-list@pauillac.inria.fr > [mailto:owner-caml-list@pauillac.inria.fr]On Behalf Of Frederic Tronel > Sent: Friday, August 23, 2002 11:46 AM > To: caml-list@inria.fr > Subject: [Caml-list] Polymorphic methods > > > Hello list, > > > I've just compiled the new 3.05 version (I will change for 3.06 > tomorrow), > and start playing a little bit with polymorphic methods. > But this small piece of code does not compile: > > class node = > object > val mutable t = None > method set_t: 'a. 'a -> unit = fun nt -> t <- Some nt > end > > File "essai.ml", line 4, characters 31-53: > This method has type 'a -> unit which is less general than 'b. 'b -> > unit > > while this one is OK (useless). > > class node = > object > val mutable t = None > method set_t: 'a. 'a -> unit = fun nt -> () > end > class node : object val mutable t : 'a option method set_t : 'b -> unit > end > > Where am I going wrong ?? > > Best regards, > > Frederic. > ------------------- > 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