From: Alain Frisch <alain@frisch.fr>
To: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Ocaml and the Fragile Base Class Problem
Date: Wed, 31 Aug 2011 23:33:08 +0200 [thread overview]
Message-ID: <4E5EA894.9060607@frisch.fr> (raw)
In-Reply-To: <61B410B8-15EF-4B18-9CC5-C224FB495353@math.nagoya-u.ac.jp>
On 8/29/2011 5:35 AM, Jacques Garrigue wrote:
> If you want just to structure your program, modules are better in most cases.
> There are still situations where classes are stronger than modules for structuring:
> * when you have a default for some operation, but want to be able to change it
> * when you want to mix components, using multiple inheritance and virtual methods
> and instance variables
I second that. We are moving some parts of our code base to a mixin
style implemented with multiple inheritance, virtual methods,
constraints, very rare overridding (and no instance variables). This
results in drastic reduction of code size compared to our previous
approach where code sharing between components was implemented with
simple function calls. The code is also easier to evolve.
We create objects (without state) to represent bags of properties
computed from parameters; each method represents a property. An object
is created by assembling reusable mixins (inheritance) and implementing
remaining virtual methods. Once the object is created, we observe a few
properties and throw the object away. In effect, we use objects to
describe purely functional computation graphs, with reusable sub-graphs.
(Most methods don't take argument; in order to avoid repeated
evaluation, we have a special hack which rewrites an object's method
table so as to give its methods a lazy behavior.)
The nice thing with this approach is that the object layer does all the
plumbing automatically. Combining several reusable components does not
require to pass data around from one component to another explicitly.
The old style with functions required to pass many arguments,
deconstruct the result (a component typically computes several things),
organize reusable components to avoid mutual recursions, and define many
data types (records to represent the result of components). In some
cases, a large amount of the code was caused by such plumbing.
> Also, for various reasons, objects are not beginner friendly.
> I think the main problem is that they don't fit that well with type inference:
> both subtyping and polymorphic methods require explicit type annotations.
Time to advertise the implicit-subtyping branch?
-- Alain
next prev parent reply other threads:[~2011-08-31 21:33 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-08-27 10:53 Chris Yocum
2011-08-27 11:24 ` Jacques Garrigue
2011-08-27 15:06 ` Gerd Stolpmann
2011-08-27 16:59 ` David Baelde
2011-08-27 19:37 ` Gerd Stolpmann
2011-08-27 20:21 ` Jeff Meister
2011-08-27 23:08 ` Gerd Stolpmann
2011-08-28 9:31 ` Andreas Rossberg
2011-08-28 10:04 ` Guillaume Yziquel
2011-08-28 10:11 ` Gerd Stolpmann
2011-08-28 10:50 ` Andreas Rossberg
2011-08-29 3:35 ` Jacques Garrigue
2011-08-29 11:19 ` Chris Yocum
2011-08-29 11:47 ` Guillaume Yziquel
2011-08-29 12:03 ` Chris Yocum
2011-08-31 21:33 ` Alain Frisch [this message]
2011-08-31 23:39 ` Jacques Garrigue
2011-08-28 17:58 ` Julien Signoles
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=4E5EA894.9060607@frisch.fr \
--to=alain@frisch.fr \
--cc=caml-list@inria.fr \
/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