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 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 891D9BE55 for ; Mon, 10 Mar 2008 09:21:01 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEAIeM1EeCNhAB/2dsb2JhbACoNw X-IronPort-AV: E=Sophos;i="4.25,473,1199660400"; d="scan'208";a="8201726" Received: from kurims.kurims.kyoto-u.ac.jp ([130.54.16.1]) by mail2-smtp-roc.national.inria.fr with ESMTP; 10 Mar 2008 09:21:00 +0100 Received: from localhost (orion [130.54.16.5]) by kurims.kurims.kyoto-u.ac.jp (8.13.8/8.13.8) with ESMTP id m2A8KoNl020080; Mon, 10 Mar 2008 17:20:50 +0900 (JST) Date: Mon, 10 Mar 2008 17:20:43 +0900 (JST) Message-Id: <20080310.172043.180101905.garrigue@math.nagoya-u.ac.jp> To: micha-1@fantasymail.de Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] oo type question From: Jacques Garrigue In-Reply-To: <200803061645.30224.micha-1@fantasymail.de> References: <200803061352.38360.micha-1@fantasymail.de> <200803060818.31857.peng.zang@gmail.com> <200803061645.30224.micha-1@fantasymail.de> X-Mailer: Mew version 4.2 on Emacs 22.1 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; donnerstag:01 someclass:01 someclass:01 syntax:01 subtyping:01 polymorphism:01 ocaml:01 peng:98 polymorphic:01 polymorphic:01 incompatible:01 caml-list:01 functions:01 functions:01 monomorphic:01 From: Michael Wohlwend > Am Donnerstag, 6. M=E4rz 2008 14:18:29 schrieb Peng Zang: > > BUT, what I think you really want is a polymorphic method, not a > > polymorphic class. The manual has a decent explanation under polym= ohrphic > > methods in the Objects section. So I think you want this: > > method add : 'a. as 'a -> unit =3D > > fun o -> ids <- o#id :: ids > = > I know that version and of course it works. I only didn't understand= (and = > still do not) why it's not possible to just declare > method add (a: #someClass) ... which seems natural for me. And it wor= ks for = > normal functions that way. > = > If you have many classes (actually it's for adding widgets to group w= idgets) = > it seems just a bit too complicated and not a natural way to declare = such a = > simple thing. One reason it is not so simple for methods is that they belong to objects. So if you write method add (a : #someClass) ... which is really equivalent to method add (a : as 'a) it is not clear where the variable should be bound. This could be either at the class level (for the whole object, as in your first example) or at the method level. So you need an explicit syntax to tell which one it is to the type system. Another reason, a bit more technical, is that polymorphic methods do not behave like polymorhic functions. In particular, they cannot be instantiated automatically when needed: i.e., you cannot pass an object with a polymorphic method where a monomorphic method is expected. Currently, even subtyping is not allowed between polymorphic methods and their monomorphics instances (this may change in 3.11). So this seems a bad idea to create a potentially incompatible polymorphic method without some explicit direction from the programmer. Note that the type annotation #someClass is not sufficient direction, because it does not request any polymorphism: it is not an error for the actual method to be of type "someClass -> ..." (I'm not sure it is what most people expect, but it is the way ocaml is defined) A small remark yet: in interfaces (and class types) it is ok to write class c : object method add : #someClass -> ... end Here it is automatically assumed that the type variable is bound at the method level. Jacques Garrigue