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.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 2952CBCE7 for ; Mon, 8 Oct 2007 10:02:09 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAADYDCUfAXQImh2dsb2JhbACOOgEBAQgKKQ X-IronPort-AV: E=Sophos;i="4.21,240,1188770400"; d="scan'208";a="2302281" Received: from discorde.inria.fr ([192.93.2.38]) by mail1-smtp-roc.national.inria.fr with ESMTP; 08 Oct 2007 01:06:25 +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 l97N6O3s026476 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Mon, 8 Oct 2007 01:06:25 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAMEACUdCbwQZmWdsb2JhbACOOgEBAQEHBAQLCBg X-IronPort-AV: E=Sophos;i="4.21,240,1188770400"; d="scan'208";a="17464992" Received: from out1.smtp.messagingengine.com ([66.111.4.25]) by mail4-smtp-sop.national.inria.fr with ESMTP; 08 Oct 2007 00:57:20 +0200 Received: from compute1.internal (compute1.internal [10.202.2.41]) by out1.messagingengine.com (Postfix) with ESMTP id 0920F2FEDF; Sun, 7 Oct 2007 18:57:20 -0400 (EDT) Received: from heartbeat1.messagingengine.com ([10.202.2.160]) by compute1.internal (MEProxy); Sun, 07 Oct 2007 18:57:20 -0400 X-Sasl-enc: 56ulPwUd8wEIQSsyV/MAMSiD1PtGDzTieNRgV1gxP42y 1191797839 Received: from [192.168.1.11] (AMontsouris-753-1-6-219.w90-2.abo.wanadoo.fr [90.2.193.219]) by mail.messagingengine.com (Postfix) with ESMTP id 5D123458B; Sun, 7 Oct 2007 18:57:19 -0400 (EDT) Date: Mon, 8 Oct 2007 00:57:09 +0200 (CEST) From: Martin Jambon X-X-Sender: martin@martin.ec.wink.com To: Luca de Alfaro Cc: caml-list@inria.fr Subject: Classes and polymorphism (Re: [Caml-list] Re: Why can't I call a function over a subclass?) In-Reply-To: <28fa90930710052153k2128bb63m5132455868eb2008@mail.gmail.com> Message-ID: References: <28fa90930710050048p3d6f4e8cs8e13d0fe49d296f2@mail.gmail.com> <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> <4706679B.9030706@gmail.com> <28fa90930710051039o7d34aaddw62b9e095215513bd@mail.gmail.com> <28fa90930710052153k2128bb63m5132455868eb2008@mail.gmail.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Miltered: at discorde with ID 47096670.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ens-lyon:01 polymorphism:01 val:01 mutable:01 subset:01 2007,:98 species:98 polymorphic:01 polymorphic:01 wrote:01 caml-list:01 inherit:01 inherit:01 monomorphic:01 int:01 On Fri, 5 Oct 2007, Luca de Alfaro wrote: > I am not so used to ":>"... looking at the example I posted, would you be > able to tell me how to make it type-check by adding ":>"? This would be > very much appreciated... Ah, sorry, I missed your second problem, which was: (next time please change the subject line and cc the list) 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 you're trying to do usually ends up being a nightmare: l would have to be polymorphic. Keeping classes polymorphic is not something practical. If you inherit an object value such as l, basically it will have one fixed type, and that's all. I find the following guidelines convenient: Define class types independently from class implementations when you know that you will have several classes of the same type. Only use monomorphic methods and values. You may inherit a class from another, and you don't need polymorphic classes for this. You never have to use inheritance. You don't have to use classes, immediate objects are often sufficient (object ... end). You can inherit and override methods, which is convenient when only a small subset of methods differ between two classes. That's when your classes are like an evolutionary tree of species. Don't bother when you have only 2 or 3 classes. Martin -- http://martin.jambon.free.fr