From: Martin Jambon <martin.jambon@ens-lyon.org>
To: Luca de Alfaro <luca@dealfaro.org>
Cc: caml-list@inria.fr
Subject: Classes and polymorphism (Re: [Caml-list] Re: Why can't I call a function over a subclass?)
Date: Mon, 8 Oct 2007 00:57:09 +0200 (CEST) [thread overview]
Message-ID: <Pine.LNX.4.64.0710080041080.24592@martin.ec.wink.com> (raw)
In-Reply-To: <28fa90930710052153k2128bb63m5132455868eb2008@mail.gmail.com>
On Fri, 5 Oct 2007, Luca de Alfaro wrote:
> I am not so used to ":>"... looking at the example I posted, would you be
> able to tell me how to make it type-check by adding ":>"? This would be
> very much appreciated...
Ah, sorry, I missed your second problem, which was:
(next time please change the subject line and cc the list)
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
What you're trying to do usually ends up being a nightmare: l would have
to be polymorphic.
Keeping classes polymorphic is not something practical.
If you inherit an object value such as l, basically it will have one fixed
type, and that's all.
I find the following guidelines convenient:
Define class types independently from class implementations when you know
that you will have several classes of the same type. Only use
monomorphic methods and values.
You may inherit a class from another, and you don't need polymorphic
classes for this.
You never have to use inheritance.
You don't have to use classes, immediate objects are often sufficient
(object ... end).
You can inherit and override methods, which is convenient when only a
small subset of methods differ between two classes. That's when your
classes are like an evolutionary tree of species. Don't bother when you
have only 2 or 3 classes.
Martin
--
http://martin.jambon.free.fr
next prev parent reply other threads:[~2007-10-08 8:02 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Martin Jambon [this message]
2007-10-05 19:48 ` 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=Pine.LNX.4.64.0710080041080.24592@martin.ec.wink.com \
--to=martin.jambon@ens-lyon.org \
--cc=caml-list@inria.fr \
--cc=luca@dealfaro.org \
/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