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.9 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 84842BBAF for ; Mon, 6 Apr 2009 01:14:44 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AngBAI7Y2EnUGyoElGdsb2JhbACWJgEBAQEJCwgJEQOxL4QPBg X-IronPort-AV: E=Sophos;i="4.39,327,1235948400"; d="scan'208";a="25688496" Received: from smtp4-g21.free.fr ([212.27.42.4]) by mail3-smtp-sop.national.inria.fr with ESMTP; 06 Apr 2009 01:14:43 +0200 Received: from smtp4-g21.free.fr (localhost [127.0.0.1]) by smtp4-g21.free.fr (Postfix) with ESMTP id 7678E4C8085 for ; Mon, 6 Apr 2009 01:14:37 +0200 (CEST) Received: from [192.168.17.1] (ivr94-8-88-162-26-239.fbx.proxad.net [88.162.26.239]) by smtp4-g21.free.fr (Postfix) with ESMTP id 52A104C803A for ; Mon, 6 Apr 2009 01:14:35 +0200 (CEST) Message-ID: <49D93BB3.501@users.sourceforge.net> Date: Mon, 06 Apr 2009 01:16:03 +0200 From: Zheng Li User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1b4pre) Gecko/20090405 Lightning/1.0pre Shredder/3.0b3pre MIME-Version: 1.0 To: OCaml Subject: Instance variables can't be polymorphic? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; val:01 iter:01 iter:01 val:01 inference:01 polymorphism:01 syntax:01 sourceforge:01 polymorphic:01 polymorphic:01 parameterize:01 functions:01 inherit:01 int:01 int:01 Hi, 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). 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. Given that I really want to use polymorphic functions this way: as instance variable and accessible through inheritance, is there any workaround or suggestions ? Thanks -- Zheng