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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 35BA0BBAF for ; Mon, 6 Apr 2009 05:41:19 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEACMX2UmCNhAB/2dsb2JhbADHc4QPBg X-IronPort-AV: E=Sophos;i="4.38,431,1233529200"; d="scan'208";a="27091436" Received: from kurims.kurims.kyoto-u.ac.jp ([130.54.16.1]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 06 Apr 2009 05:41:17 +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 n363f8gH029435; Mon, 6 Apr 2009 12:41:08 +0900 (JST) Date: Mon, 06 Apr 2009 12:40:55 +0900 (JST) Message-Id: <20090406.124055.116565018.garrigue@math.nagoya-u.ac.jp> To: zheng_li@users.sourceforge.net Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Instance variables can't be polymorphic? From: Jacques Garrigue In-Reply-To: <49D93BB3.501@users.sourceforge.net> References: <49D93BB3.501@users.sourceforge.net> X-Mailer: Mew version 4.2 on Emacs 22.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; val:01 iter:01 iter:01 val:01 inference:01 polymorphism:01 syntax:01 polymorphism:01 syntax:01 sourceforge:01 polymorphic:01 polymorphic:01 parameterize:01 caml-list:01 functions:01 From: Zheng Li > Here is an example: > > ---- > # class c = object > val iter = List.iter > end;; > class c : object val iter : ('a -> unit) -> 'a list -> unit end > ---- > > Since iter is a instance variable, the type parameter 'a won't be > required to parameterize the type of the class, perfect! > > But it's still not polymorphic. > > ---- > # let o = object > inherit c > method do_sth = iter print_int []; iter print_string [] > end;; > Characters 69-81: > method do_sth = iter print_int []; iter print_string [] > ^^^^^^^^^^^^ > Error: This expression has type string -> unit but is here used with type > int -> unit > ---- > > Is that reasonable? The inference of class c is done before the > declaration of object o, and the type signature says it's polymorphic > (not a weak one '_a). Actually it's polymorphic, but only at the level of inheritance. You could define two objects inheriting from c, one using print_int, and the other print_string, and this would work. Due to functional update (i.e. {< iter = ... >}), object-level polymorphism would have to be explicit, but as you pointed next, no syntax is provided for explicitly polymorphic fields. > Trying to declare the polymorphism explicitly as > > -- > val iter : 'a. ('a -> unit) -> 'a list -> unit = List.iter > -- > > won't work. This syntax is only allowed for methods. Yes. It would be too difficult to add such a functionality, but what kind of application do you have in mind? > Given that I really want to use polymorphic functions this way: as > instance variable and accessible through inheritance, is there any > workaround or suggestions ? The simplest way I see currently is to use a let defined field (i.e., before the object keyword, but then you can't access it after inheritance), or to use a private method. What is your problem with a private method? It should be more flexible. Of course you can also define a record to wrap your polymorphic value. Jacques Garrigue