caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Re: OCAML Downcasting?
Date: 22 Sep 2004 10:50:34 +1000	[thread overview]
Message-ID: <1095814233.2580.921.camel@pelican.wigram> (raw)
In-Reply-To: <Pine.LNX.4.44.0409211619370.5809-100000@localhost.localdomain>

On Wed, 2004-09-22 at 07:38, Brian Hurt wrote:

> Downcasting is a sign you're doing something wrong.  If you knew the 
> object was a bar_t, why didn't it have the type bar_t from the get-go?  

I agree. However I'll add two explanations here.

(1) C++ and Java classes use nominal typing which
means they have a particular name. Two similar classes
with a different name are different. This is important
because the associated type is determined exclusively
by the name.

In Ocaml, the class name is irrelevant because they use
structural typing. This means that what counts is the
set of names of the methods and their types. So in Ocaml
two similar classes with the different names have the
*same* associated type. Of course you can name such
types, but the names are just aliases.

Structural typing is prone to errors where two
types intended to be distinct become interchangeable
"by accident" because they have the same method names and types
"by accident" but in practice this hardly ever happens.

OTOH structural typing is vastly superior to nominal
typing when it comes to subtyping. Algorithms can
be written requiring a particular type, and can be
applied to any class that has that type as a supertype,
just as in Java or C++. The difference is you don't
have to invade the class definition and add the name
of the supertype to the list of abstract bases/interfaces.

Therefore, the most fundamental principle of Object
Orientation -- Meyers Open/Closed principle -- 
is obeyed by Ocaml classes and NOT by C++ or Java.
It is this principle which helps provide encapsulation.

You could say Ocaml is more Object Oriented
than either C++ or Java because it obeys the
major principles better.

(2) In Ocaml we have two kinds of unions: ordinary
variants and polymorphic variants, aka sum types.

In C++ and Java, we don't have these types natively.
[C++ has a severely broken union]

So what programmers do to emulate them is use
inheritence. This is the primary reason both
C++ and java require downcasts.

So in summary: OO programmers are generally ignorant
of the very fundamentals of programming theory.
They're used to  dealing with all sorts of weird
problems representing simple data which arises
directly out from broken type systems C++ and Java
provide: a naturally algebraic representation
can't be found, because they only provide
products and not sums. So the programmer
turns to abstraction, but then has to
'unabstract' the abstraction using downcasts.

The worst thing about this is that inheritance
is used to provide two fundamentally distinct type
relations: abstraction/subtyping and unification.
In Ocaml, there are two distinct mechanisms.

No one would claim Ocaml is perfect, but it does
get the basics right. Note I didn't mention
the fact you also have first class function
types: this has nothing to do with functional
programming per se.

Its just that languages like Ocaml were designed
after doing basic mathematics, Java and C++
were not.

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2004-09-22  0:50 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <ci7tcf$qqf$1@wolfberry.srv.cs.cmu.edu>
     [not found] ` <ci9ggm$i6p$1@wolfberry.srv.cs.cmu.edu>
2004-09-21  8:03   ` Jacques GARRIGUE
2004-09-21  8:43     ` Damien Pous
2004-09-21  9:15       ` Jacques GARRIGUE
2004-09-21  9:29         ` skaller
2004-09-21  9:49           ` Jacques GARRIGUE
2004-09-21  9:34         ` Stefano Zacchiroli
2004-09-21  9:56           ` Jacques GARRIGUE
2004-09-21 19:27     ` Michael Vanier
2004-09-21 21:38       ` Brian Hurt
2004-09-21 22:06         ` Michael Vanier
2004-09-21 22:32           ` Brian Hurt
2004-09-22  1:04           ` skaller
2004-09-21 22:20         ` Marcin 'Qrczak' Kowalczyk
2004-09-22  2:26           ` skaller
2004-09-22  6:31             ` Marcin 'Qrczak' Kowalczyk
2004-09-22  9:03               ` sejourne_kevin
2004-09-22 10:29               ` Richard Jones
2004-09-22 18:39                 ` Brian Hurt
2004-09-22 10:50               ` skaller
2004-09-22 12:03               ` Alain Frisch
2004-09-22 12:50               ` Cláudio Valente
2004-09-22 13:15                 ` Marcin 'Qrczak' Kowalczyk
2004-09-22 15:50                   ` skaller
2004-09-22 18:42               ` Brian Hurt
2004-09-22 18:44                 ` Marcin 'Qrczak' Kowalczyk
2004-09-22 19:18                   ` Brian Hurt
2004-09-22  0:50         ` skaller [this message]
2004-09-22  1:30       ` Jacques GARRIGUE
2004-09-22  2:59         ` skaller

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=1095814233.2580.921.camel@pelican.wigram \
    --to=skaller@users.sourceforge.net \
    --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;
as well as URLs for NNTP newsgroup(s).