From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: STARYNKEVITCH Basile <Basile.Starynkevitch@cea.fr>, caml-list@inria.fr
Subject: Re: Real excellent object oriented source code examples of Ocaml
Date: Mon, 20 Nov 2000 14:46:31 +0100 [thread overview]
Message-ID: <20001120144631.30106@pauillac.inria.fr> (raw)
In-Reply-To: <14869.8506.203392.805459@gargle.gargle.HOWL>; from STARYNKEVITCH Basile on Fri, Nov 17, 2000 at 01:14:50PM +0100
> Franck wants to see real good Ocaml-3 code using all -or most of- the
> fancy advanced object-oriented features (with multiple inheritance,
> class types, functors, etc...). And I did not found much of such code
> (e.g. OO features are almost unused in the Ocaml compiler).
Not quite. The native-code compiler uses classes, inheritance and
method overriding quite a lot to express target-specific optimizations.
(See below.)
> I am trying to show him that most of the advanced features of C++
> (templates, overloading, multiple inheritance) have their equivalent
> in Ocaml thru real source code (or that they are less useful, like C++
> overloading).
I don't want to be picky, but C++ templates are essentially unrelated to
OO programming, and correspond most closely to OCaml's functors, along
with higher-order functions and core-language polymorphism for simpler
cases. There are plenty of nice examples of the latter (core language
polymorphism and HO functions). As for functors, the "Set" and "Map"
modules from the standard library are good representative examples, I
think. But then you can also add "excellent functorized source code
examples" to the subject of your message.
To follow up on Don Syme's reply, examples that demonstrate the
interest of class-based programming, as well as functorized
programming, are hard to find because most closed, stand-alone
applications do not really need classes nor functors. That is, for
such applications, it is almost always possible to come up with a good
implementation that does not use either classes nor functors, and
indeed such "simple" implementations are often no larger and easier to
read than a class-based or heavily functorized implementation.
To feel the need for classes and functors, you need to look at
code that needs to remain flexible, extensible and reusable in
contexts that were not fully known at the time it was written. For
instance: libraries, application frameworks, extensible applications
where new stuff will have to be added throughout the life of the
application.
To pick my favorite example, a retargetable native-code compiler needs
to be extended each time support for a new processor is added. The
new processor can look very much like an already supported one, in
which case it should be possible to reuse a lot of existing code; but
it can also be quite different (e.g. adding an IA32 back-end to a
compiler that supports only RISC processors), in which case it should
be possible to override significant parts of the existing code, but
without rewriting everything from scratch. I found OCaml classes
quite effective to handle this in the ocamlopt compiler, actually more
handy than functors.
Others on this list can certainly contribute similar examples, but
they tend to be large and hard to comprehend -- just because what they
do is quite complex, and because you have to keep in mind their
evolution over time.
To come back to your original question: why don't you first look at
the kind of things your colleague does in C++ and see whether it
couldn't be expressed nicely using e.g. datatypes, pattern-matching,
polymorphism and HO functions? Quite often, Java and C++ programmers
program in object-oriented style not because that's the best style for
their application, but because it's the only style supported by the
language. E.g. they are used to write N+1 classes to encode a
N-constructor datatype, just because it's the way it's done in Java or
C++.
A good thing about having objects and classes in OCaml, in addition to
addressing those relatively few programming tasks that really need
them, is that when someone comes to you and asks "Can you do this
<insert tangle of classes here> in OCaml?", you can often answer "Yes,
I can do this with classes just like in your original code; but I can
also do it without, and see how much clearer and more readable it
gets". OCaml as a rehabilitation clinic for OO programmers, if you
wish...
Good luck,
- Xavier Leroy
next prev parent reply other threads:[~2000-11-20 19:28 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-11-17 12:14 STARYNKEVITCH Basile
2000-11-20 13:46 ` Xavier Leroy [this message]
2000-11-23 19:32 ` Anton Moscal
2000-11-28 14:12 ` Renaud.Rioboo
2000-11-17 14:38 Don Syme
2000-11-19 15:48 ` Mattias Waldau
2000-11-20 20:43 David McClain
2000-11-21 16:49 ` Brian Rogoff
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=20001120144631.30106@pauillac.inria.fr \
--to=xavier.leroy@inria.fr \
--cc=Basile.Starynkevitch@cea.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