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.4 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE autolearn=disabled version=3.1.3 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id B0146BC6B for ; Tue, 9 Oct 2007 06:19:21 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAE+eCkfAXQImh2dsb2JhbACORgEBAQgKKQ X-IronPort-AV: E=Sophos;i="4.21,246,1188770400"; d="scan'208";a="4099906" Received: from discorde.inria.fr ([192.93.2.38]) by mail3-smtp-sop.national.inria.fr with ESMTP; 09 Oct 2007 06:19:19 +0200 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l994JHbv011405 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Tue, 9 Oct 2007 06:19:18 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao8CANadCkeCNhAB/2dsb2JhbAA X-IronPort-AV: E=Sophos;i="4.21,246,1188770400"; d="scan'208";a="17628671" Received: from kurims.kurims.kyoto-u.ac.jp ([130.54.16.1]) by mail4-smtp-sop.national.inria.fr with ESMTP; 09 Oct 2007 06:19:15 +0200 Received: from localhost (orion [130.54.16.5]) by kurims.kurims.kyoto-u.ac.jp (8.13.8/8.13.8) with ESMTP id l994J9CY002542; Tue, 9 Oct 2007 13:19:09 +0900 (JST) Date: Tue, 09 Oct 2007 13:18:57 +0900 (JST) Message-Id: <20071009.131857.128607068.garrigue@math.nagoya-u.ac.jp> To: luca@dealfaro.org Cc: caml-list@inria.fr Subject: Re: [Caml-list] Re: Why can't I call a function over a subclass? From: Jacques Garrigue In-Reply-To: <28fa90930710050849y64f2cc5en16d15c80c5576a61@mail.gmail.com> References: <28fa90930710050812i204e975ap847e7474ec9580bc@mail.gmail.com> <20071005152130.M41697@cs.unm.edu> <28fa90930710050849y64f2cc5en16d15c80c5576a61@mail.gmail.com> X-Mailer: Mew version 4.2 on Emacs 22.1 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 470B0145.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; val:01 mutable:01 val:01 mutable:01 caml-list:01 inherit:01 inherit:01 int:01 int:01 garrigue:03 garrigue:03 subclass:03 jacques:03 jacques:03 unit:03 From: "Luca de Alfaro" > 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 Zheng Li already provided an answer to your problem. However Zheng's answer, while doing exactly what you were trying to do, use virtual value fields, which are a new feature in 3.10. A more standard way to do this would be to use a private virtual method, as make_el is the same across all instances of a specific class. class virtual r = object (self) val mutable l = [] method private virtual make_el : int -> #p method add x = l <- self#make_el x ::l method length = List.length l method total = List.fold_left (fun t el -> t + el#plus1) 0 l end class r1 = object inherit r method make_el = new p end class r2 = object inherit r method make_el = new p2 method total2 = List.fold_left (fun t el -> t + el#plus2) 0 l end Practically, these two solutions are equivalent. Jacques Garrigue