Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Re: OCAML Downcasting?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-09-22 (00:50)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Re: OCAML Downcasting?
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,
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: