From: "Luca de Alfaro" <luca@dealfaro.org>
To: "Edgar Friendly" <thelema314@gmail.com>
Cc: "William D. Neumann" <wneumann@cs.unm.edu>,
"Inria Ocaml Mailing List" <caml-list@inria.fr>
Subject: Re: [Caml-list] Re: Why can't I call a function over a subclass?
Date: Fri, 5 Oct 2007 10:39:03 -0700 [thread overview]
Message-ID: <28fa90930710051039o7d34aaddw62b9e095215513bd@mail.gmail.com> (raw)
In-Reply-To: <4706679B.9030706@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2485 bytes --]
I thank you all for the help... in the end, I solved the practical problem
by collapsing p and p2 into the same class -- not so nice, but it works, and
it is simpler than other solutions.
But the larger issue is: when a type is used for an input parameters, as the
type p in:
f : int -> p -> bool
why does the compiler have to complain that p has too many methods?
Would it not be possible to make the type checker smarter, and ensure that
when p is an input type, it is ok to have more methods?
And for output types as well, it would seem?
In other words, what is that fundamentally breaks if one were to change the
ocaml type checker in this way?
Luca
On 10/5/07, Edgar Friendly <thelema314@gmail.com> wrote:
>
> Luca de Alfaro wrote:
> > Yes, here is some code. Any help would be very much appreciated.
> > The following fails to type check:
> >
> > class p (x: int) = object
> > method plus1 : int = x + 1
> > end
> >
> > class p2 (x: int) = object
> > inherit p x
> > method plus2 : int = x + 2
> > end
> >
> > class r = object (self)
> > val mutable l = []
> > method make_el x = new p x
> > method add (x: int) : unit = l <- (self#make_el x) :: l
> > method length : int = List.length l
> > method total : int = List.fold_left (fun t el -> t + el#plus1) 0 l
> > end
> >
> > class r2 = object
> > inherit r
> > method make_el x = new p2 x
> > method total2 : int = List.fold_left (fun t el -> t + el#plus2) 0 l
> > end
> >
> if I manually perform the inherit operation by pasting the code from r1
> into r2, I get:
>
> class r2 = object (self)
> val mutable l = []
> method make_el x = new p2 x
> method add (x: int) : unit = l <- (self#make_el x) :: l
> method length : int = List.length l
> method total2 : int = List.fold_left (fun t el -> t + el#plus2) 0 l
> end
>
> which compiles just fine, and probably works as intended. If I include
> the original method make_el above the new one like this:
> method make_el x = new p x
> method make_el x = new p2 x
> Ignoring the warning about overriding methods within the same class, we
> come to the root of the type problem: make_el must have a type. After
> inference completes on the first line, make_el's type is determined to
> be p. The second make_el's type must match, but it doesn't. I don't
> see a solution for your problem that doesn't involve this kind of manual
> expansion and removal of duplicate methods, but I'm fairly sure this is
> the real problem for you.
>
> E.
>
[-- Attachment #2: Type: text/html, Size: 3209 bytes --]
next prev parent reply other threads:[~2007-10-05 17:39 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-05 7:48 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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=28fa90930710051039o7d34aaddw62b9e095215513bd@mail.gmail.com \
--to=luca@dealfaro.org \
--cc=caml-list@inria.fr \
--cc=thelema314@gmail.com \
--cc=wneumann@cs.unm.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox