From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=AWL,HTML_MESSAGE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id E71BFBC6C for ; Fri, 5 Oct 2007 17:49:20 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAFX5BUfAXQImh2dsb2JhbACCOzaLSAIBCAop X-IronPort-AV: E=Sophos;i="4.21,236,1188770400"; d="scan'208";a="2427061" Received: from discorde.inria.fr ([192.93.2.38]) by mail2-smtp-roc.national.inria.fr with ESMTP; 05 Oct 2007 17:49:20 +0200 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l95FnCv0021359 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 5 Oct 2007 17:49:20 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAAb6BUdC+VLmmGdsb2JhbACCOzaLSAIBAQcEBBMY X-IronPort-AV: E=Sophos;i="4.21,236,1188770400"; d="scan'208";a="2193808" Received: from wx-out-0506.google.com ([66.249.82.230]) by mail1-smtp-roc.national.inria.fr with ESMTP; 05 Oct 2007 17:49:19 +0200 Received: by wx-out-0506.google.com with SMTP id h28so564879wxd for ; Fri, 05 Oct 2007 08:49:18 -0700 (PDT) Received: by 10.142.229.4 with SMTP id b4mr1614293wfh.1191599357931; Fri, 05 Oct 2007 08:49:17 -0700 (PDT) Received: by 10.142.80.4 with HTTP; Fri, 5 Oct 2007 08:49:17 -0700 (PDT) Message-ID: <28fa90930710050849y64f2cc5en16d15c80c5576a61@mail.gmail.com> Date: Fri, 5 Oct 2007 08:49:17 -0700 From: "Luca de Alfaro" To: "William D. Neumann" , "Inria Ocaml Mailing List" Subject: Re: [Caml-list] Re: Why can't I call a function over a subclass? In-Reply-To: <20071005152130.M41697@cs.unm.edu> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_28463_8310940.1191599357924" References: <28fa90930710050048p3d6f4e8cs8e13d0fe49d296f2@mail.gmail.com> <4705EF58.3040605@bik-gmbh.de> <1191580233.6509.26.camel@Blefuscu> <87ejg9oms3.fsf@pps.jussieu.fr> <1191592931.6509.33.camel@Blefuscu> <28fa90930710050759m682d8b65g72a31cc311acc276@mail.gmail.com> <28fa90930710050812i204e975ap847e7474ec9580bc@mail.gmail.com> <20071005152130.M41697@cs.unm.edu> X-j-chkmail-Score: MSGID : 47065CF8.000 on discorde : j-chkmail score : XX : 5/20 0 0.000 -> 2 X-Miltered: at discorde with ID 47065CF8.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; val:01 mutable:01 compiler:01 runtime:01 ocaml:01 casts:01 notations:01 val:01 mutable:01 compiler:01 runtime:01 ocaml:01 casts:01 notations:01 complains:01 ------=_Part_28463_8310940.1191599357924 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Yes, here is some code. Any help would be very much appreciated. The following fails to type check: class p (x: int) = object method plus1 : int = x + 1 end class p2 (x: int) = object inherit p x method plus2 : int = x + 2 end class r = object (self) val mutable l = [] method make_el x = new p x method add (x: int) : unit = l <- (self#make_el x) :: l method length : int = List.length l method total : int = List.fold_left (fun t el -> t + el#plus1) 0 l end class r2 = object inherit r method make_el x = new p2 x method total2 : int = List.fold_left (fun t el -> t + el#plus2) 0 l end What I am trying to do is to replace, in l, elements of type p with elements of type p2, so that my class r2 can offer more methods (or use them internally). However, the compiler complains that p2 and p are not compatible, because p does not have method plus2 (in spite of the fact that no runtime error can occur). I am not a deep expert of Ocaml type system for classes, as you might have guessed by now, so if someone were able to suggest a set of type casts/notations/... that would make the above go through, I would be very grateful. In my real (large) code, I use the elements of the list l in many places inside the code of r (and by inheritance, r2). Do you have any code that we can look at to see what the problem is? Can > you perhaps construct a small yet complete example that demonstartes the > issue? > > -- > > William D. Neumann > > ------=_Part_28463_8310940.1191599357924 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Yes, here is some code.  Any help would be very much appreciated.
The following fails to type check:

class p (x: int) = object
  method plus1 : int = x + 1
end

class p2 (x: int) = object
  inherit p x
  method plus2 : int = x + 2
end

class r = object (self)
  val mutable l = []
  method make_el x = new p x
  method add (x: int) : unit = l <- (self#make_el x) :: l
  method length : int = List.length l
  method total : int = List.fold_left (fun t el -> t + el#plus1) 0 l 
end

class r2 = object
  inherit r
  method make_el x = new p2 x
  method total2 : int = List.fold_left (fun t el -> t + el#plus2) 0 l 
end

What I am trying to do is to replace, in l, elements of type p with elements of type p2, so that my class r2 can offer more methods (or use them internally).  However, the compiler complains that p2 and p are not compatible, because p does not have method plus2 (in spite of the fact that no runtime error can occur).

I am not a deep expert of Ocaml type system for classes, as you might have guessed by now, so if someone were able to suggest a set of type casts/notations/... that would make the above go through, I would be very grateful.
In my real (large) code, I use the elements of the list l in many places inside the code of r (and by inheritance, r2).





Do you have any code that we can look at to see what the problem is?  Can
you perhaps construct a small yet complete example that demonstartes the
issue?

--

William D. Neumann


------=_Part_28463_8310940.1191599357924--