caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Why can't I call a function over a subclass?
@ 2007-10-05  7:48 Luca de Alfaro
  2007-10-05  8:01 ` [Caml-list] " Florian Hars
                   ` (2 more replies)
  0 siblings, 3 replies; 22+ messages in thread
From: Luca de Alfaro @ 2007-10-05  7:48 UTC (permalink / raw)
  To: Inria Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 1155 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 1413 bytes --]

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2007-10-09  4:19 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-05  7:48 Why can't I call a function over a subclass? Luca de Alfaro
2007-10-05  8:01 ` [Caml-list] " Florian Hars
2007-10-05  8:08   ` Luca de Alfaro
2007-10-05  8:08     ` Fwd: " Luca de Alfaro
2007-10-05 11:08     ` Vincent Aravantinos
2007-10-05 11:47       ` Christophe Raffalli
2007-10-05 10:30   ` David Teller
2007-10-05 10:53     ` Zheng Li
2007-10-05 14:02       ` [Caml-list] " David Teller
2007-10-05 14:59         ` Luca de Alfaro
2007-10-05 15:12           ` Luca de Alfaro
     [not found]             ` <20071005152130.M41697@cs.unm.edu>
2007-10-05 15:49               ` Luca de Alfaro
2007-10-05 16:34                 ` Edgar Friendly
2007-10-05 17:39                   ` Luca de Alfaro
2007-10-05 17:49                     ` Martin Jambon
     [not found]                       ` <28fa90930710052153k2128bb63m5132455868eb2008@mail.gmail.com>
2007-10-07 22:19                         ` Martin Jambon
2007-10-07 22:57                         ` Classes and polymorphism (Re: [Caml-list] Re: Why can't I call a function over a subclass?) Martin Jambon
2007-10-05 19:48                 ` Why can't I call a function over a subclass? Zheng Li
2007-10-06  1:49                   ` [Caml-list] " Jake Donham
2007-10-09  4:18                 ` Jacques Garrigue
2007-10-05  8:07 ` [Caml-list] " Pietro Abate
2007-10-05 10:55 ` Andrej Bauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).