From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id SAA02993 for caml-red; Thu, 11 Jan 2001 18:39:55 +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 OAA28511 for ; Thu, 11 Jan 2001 14:09:25 +0100 (MET) Received: from isis.lip6.fr (isis.lip6.fr [132.227.60.2]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f0BD9Pj19086 for ; Thu, 11 Jan 2001 14:09:25 +0100 (MET) Received: from spi.lip6.fr (IDENT:root@spi.lip6.fr [132.227.83.55]) by isis.lip6.fr (8.11.0/jtpda-5.3.2) with ESMTP id f0BD9NV21878 ; Thu, 11 Jan 2001 14:09:23 +0100 Received: from baltazard.lip6.fr (root@baltazard.lip6.fr [132.227.82.102]) by spi.lip6.fr (8.8.7/jtpda-5.2) with ESMTP id OAA07156 ; Thu, 11 Jan 2001 14:09:29 +0100 Received: from (boulme@localhost) by baltazard.lip6.fr (8.9.3/jtpda-5.2) id OAA02994 ; Thu, 11 Jan 2001 14:09:21 +0100 Message-Id: <200101111309.OAA02994@baltazard.lip6.fr> X-Mailer: exmh version 2.1.1 10/15/1999 To: "Mattias Waldau" Cc: caml-list@inria.fr Subject: Re: Why can't I use val mover : < move : int -> unit; .. > list -> unit ? In-Reply-To: Your message of "Thu, 11 Jan 2001 10:07:33 +0100." Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Thu, 11 Jan 2001 14:09:21 +0100 From: "Sylvain BOULM'E" Sender: weis@pauillac.inria.fr > I understand the principle that the compiler must be sure that everywhere I > use the list, the elements will be of the right type. In this case each > element must have the method move. > > However, it seems to me that in this case the compiler has already done the > complex part, i.e. realizing that if all objects have a move-method, then > everything is ok. So I don't see the point that I have to coerce explicitily > when putting elements into the list. > Such a coercion is not always possible: forget the "unnecessary" methods has not always a meaning. For instance, let's have an example a bit more complex than yours : In the context : class pt = object (_:'a) method compare (x:'a) = true end class qt (i:int) = object (s:'a) val mutable state = i method compare (x:'a) = x#get = s#get method get = state end let p = new pt and q1 = new qt 1 and q2 = new qt 2;; (** test if x is comparable to all elements of l **) (* val compare_iter : < compare : 'a -> bool; .. > list -> 'a -> bool *) let compare_iter l x = List.fold_left (fun b y -> b && y#compare x) true l;; compare_iter [p;p] p;; compare_iter [q1;q2] q1;; Both these expressions below are not well-typed : compare_iter [q1;q2] p;; compare_iter [(q1:> pt);(q2:> pt)] p;; Indeed, an object of type "qt" can not be coerced to an object of type "pt" because an object of type "qt" needs an object with a "get" method as argument for "compare". What is interesting here with ocaml polymorphism, is that you can however have a function as "compare_iter" which can both handle "lists of pt" and "lists of qt". Hope this helps, Sylvain.