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=1.0 required=5.0 tests=AWL,SPF_FAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 6E541BC6C for ; Fri, 5 Oct 2007 21:47:13 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAFYxBkfAXQInh2dsb2JhbACOOQEBAQgKKQ X-IronPort-AV: E=Sophos;i="4.21,237,1188770400"; d="scan'208";a="17349425" Received: from concorde.inria.fr ([192.93.2.39]) by mail4-smtp-sop.national.inria.fr with ESMTP; 05 Oct 2007 21:47:13 +0200 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l95JlCxU017638 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 5 Oct 2007 21:47:12 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAFYxBkdQW+UCh2dsb2JhbACOOQEBAQgKKQ X-IronPort-AV: E=Sophos;i="4.21,237,1188770400"; d="scan'208";a="2201763" Received: from main.gmane.org (HELO ciao.gmane.org) ([80.91.229.2]) by mail1-smtp-roc.national.inria.fr with ESMTP; 05 Oct 2007 21:47:12 +0200 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1Idt8G-0002J2-N4 for caml-list@inria.fr; Fri, 05 Oct 2007 19:46:45 +0000 Received: from ivr94-8-88-162-26-239.fbx.proxad.net ([88.162.26.239]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 05 Oct 2007 19:46:44 +0000 Received: from li by ivr94-8-88-162-26-239.fbx.proxad.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 05 Oct 2007 19:46:44 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Zheng Li Subject: Re: Why can't I call a function over a subclass? Date: Fri, 05 Oct 2007 21:48:33 +0200 Message-ID: <878x6hny0e.fsf@pps.jussieu.fr> 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> <28fa90930710050849y64f2cc5en16d15c80c5576a61@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: ivr94-8-88-162-26-239.fbx.proxad.net User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.50 (gnu/linux) Cancel-Lock: sha1:JDZQx+kn/Omd2kN78JvdvGoM5rs= Sender: news X-Miltered: at concorde with ID 470694C0.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; val:01 mutable:01 val:01 mutable:01 arbitrarily:01 extensible:01 pps:01 pps:01 inherit:01 inherit:01 jussieu:01 jussieu:01 int:01 int:01 writes:01 "Luca de Alfaro" writes: > 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 I tried it in another way, maybe not the way you expect, but just for your reference. class virtual r = object val mutable l = [] val virtual make_el: int -> #p method add x = l <- 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 val make_el = new p end class r2 = object inherit r val make_el = new p2 method total2 = List.fold_left (fun t el -> t + el#plus2) 0 l end Note that before you finally provide the virtual classes with a concrete make_el, you're free to inherit and extend them *arbitrarily*. E.g. class virtual r3 = object inherit r method total3 = List.fold_left (fun t el -> t + el#plus3) 0 l end class virtual r4 = object inherit r3 (* r3 is extensible *) method total4 = List.fold_left (fun t el -> t + el#plus4) 0 l end HTH. -- Zheng Li http://www.pps.jussieu.fr/~li