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.7 required=5.0 tests=AWL,HTML_10_20,HTML_MESSAGE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr 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 EBDB8BC69 for ; Fri, 5 Oct 2007 09:50:52 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAEqJBUfAXQInh2dsb2JhbACCOjeLRwIBCAop X-IronPort-AV: E=Sophos;i="4.21,234,1188770400"; d="scan'208";a="3782733" Received: from concorde.inria.fr ([192.93.2.39]) by mail3-smtp-sop.national.inria.fr with ESMTP; 05 Oct 2007 09:50:52 +0200 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l957onWJ027183 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 5 Oct 2007 09:50:52 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAP+JBUfRVca+iGdsb2JhbACCOjeLRwIBCAgTFg X-IronPort-AV: E=Sophos;i="4.21,234,1188770400"; d="scan'208";a="17323389" Received: from rv-out-0910.google.com ([209.85.198.190]) by mail4-smtp-sop.national.inria.fr with ESMTP; 05 Oct 2007 09:49:06 +0200 Received: by rv-out-0910.google.com with SMTP id f5so34874rvb for ; Fri, 05 Oct 2007 00:48:55 -0700 (PDT) Received: by 10.142.14.20 with SMTP id 20mr1531729wfn.1191570534949; Fri, 05 Oct 2007 00:48:54 -0700 (PDT) Received: by 10.142.80.4 with HTTP; Fri, 5 Oct 2007 00:48:54 -0700 (PDT) Message-ID: <28fa90930710050048p3d6f4e8cs8e13d0fe49d296f2@mail.gmail.com> Date: Fri, 5 Oct 2007 00:48:54 -0700 From: "Luca de Alfaro" To: "Inria Ocaml Mailing List" Subject: Why can't I call a function over a subclass? MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_26787_25665192.1191570534690" X-Miltered: at concorde with ID 4705ECD9.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 bool:01 compiler:01 bool:01 ocaml:01 compiler:01 complains:01 complains:01 inherit:01 inherit:01 int:01 int:01 mly:01 expression:02 expression:02 ------=_Part_26787_25665192.1191570534690 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Consider the following sample code, that refuses to type check in Ocaml: ---start--- class r (x_init: int) = object method get_x : int = x_init end let f (r1: r) (r2: r) : bool = (r1#get_x = r2#get_x) class r' (x_init: int) = object inherit r x_init method get_xx : int = 2 * x_init end let q (r1: r') (r2: r') = f r1 r2 ---end--- r' is a subclass of r. The compiler complains: File "class1.ml", line 12, characters 28-30: This expression has type r' but is here used with type r The second object type has no method get_xx Now, I know r does not have method get_xx, but why on Earth should this matter? I can see that there can be an error if I feed to f something that has FEWER methods, but why should it be an error to feed to f something with MORE methods?? The absurdity continues: if I instead declare f as: let f (r1: ) (r2: ) : bool = (r1#get_x = r2#get_x) then the code type-checks. Why is this fine but f: r -> r -> bool causes a type error? The situation does not make sense to me. Can someone shed some light on what is going on? (This with Ocaml 3.10.0 in case it matters) Best, Luca ------=_Part_26787_25665192.1191570534690 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Consider the following sample code, that refuses to type check in Ocaml:

---start---

class r (x_init: int) = object
  method get_x : int = x_init
end

let f (r1: r) (r2: r) : bool = (r1#get_x = r2#get_x)

class r' (x_init: int) = object
  inherit r x_init
  method get_xx : int = 2 * x_init
end

let q (r1: r') (r2: r') = f r1 r2

---end---

r' is a subclass of r.  The compiler complains:

File "class1.ml", line 12, characters 28-30:
This expression has type r' but is here used with type r
The second object type has no method get_xx

Now, I know r does not have method get_xx, but why on Earth should this matter?
I can see that there can be an error if I feed to f something that has FEWER methods, but why should it be an error to feed to
f something with MORE methods??

The absurdity continues: if I instead declare f as:

let f (r1: <get_x: int; ..>) (r2: <get_x: int; ..>) : bool =
  (r1#get_x = r2#get_x)

then the code type-checks.  Why is this fine but f: r -> r -> bool causes a type error?
The situation does not make sense to me.  Can someone shed some light on what is going on?
(This with Ocaml 3.10.0 in case it matters)

Best,

Luca

------=_Part_26787_25665192.1191570534690--