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-21 (19:27)
From: Michael Vanier <mvanier@c...>
Subject: Re: [Caml-list] Re: OCAML Downcasting?

> Date: Tue, 21 Sep 2004 17:03:39 +0900 (JST)
> From: Jacques GARRIGUE <>
> There are two important differences between ocaml and other
> OO-languages that have downcasting.
> - inheritance is not subtyping. The subtyping relation being
>   structural, a full fledge typecast would not only require to keep
>   type information at runtime, but to embark a large part of the
>   compiler to check subtyping. This might also be incredibly slow.
>   As a result, downcast is only reasonable when we have both
>   inheritance and subtyping. You're suddenly losing a large part of
>   the language.

But java has interfaces (subtypes), and classes can implement interfaces
(making them subtypes of the interface) without inheritance.  Furthermore,
the class identity can be retrieved from an instance of an interface by
using the instanceof operator.

>   Worse, even if you have a module interface
>      class b :
>        object
>          inherit a
>          ....
>        end
>   in ocaml this does _not_ mean that b inherits from a, just that it
>   has at least a's methods and fields.
>   So the semantics of downcast would not allow abstraction.

I don't understand the point you're trying to make here, but it seems like
a is effectively equivalent to an interface in java (at least from your

> - classes may have type parameters. Assume a situation like
>     class ['a] a = object ... end
>   Then we cannot downcast any runtime object to "a", because we don't
>   know what is its type parameter. Again, downcast would not mix well
>   with other features of the language.

Java now has generics (type parameters), and I assume that it has to get
around this situation somehow.

> So, while this is somehow unfortunate, there is no downcast in ocaml.

I'm sure there are good reasons for this, but I don't find the arguments
you've presented above persuasive.  Not that I'm trying to hold java up as
a shining example of the Right Thing; I'd much rather program in ocaml than
java any day of the week.  But the lack of downcasting has frustrated me in
the past (it's my #1 gripe about ocaml, with the lack of support for
native-code shared libraries at #2).


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