English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] dynamic runtime cast in Ocaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-11-06 (11:53)
From: Basile STARYNKEVITCH <basile@s...>
Subject: [Caml-list] dynamic runtime cast in Ocaml
Dear Ocaml developers

I am sometimes lacking the dynamic cast (with a runtime test) in
Ocaml. Assuming that I have the usual point and colored_point classes
(as in the 3.06 Manual), I want to deal with point array-s and test if
the 3rd point is in fact a colored point.

Is there some type-theoritical issues here? I thought a few minutes
(assuming a single-inheritance world) and found none, but I am not a
type theorist and I am (sadly) not working at INRIA. Perhaps
parameterized classes and constraints are an issue... But we ordinary
people need some explanations about it..

Is there some implementation issues here? There could be some with
multiple-inheritance, but I think that dynamic cast could be
restricted to a single-inheritance sub-world, and the classical
ObjVlisp model offer a quick runtime test for this. BTW, I believe
that the existing runtime has most of the features required for such a

Is there some syntax issues here? I suggest using the pattern matching
with a object keyword, so

(* test if a point is in fact a colored one *)
let point_is_colored (pt: point) = match pt with
   object colored_point -> true
  | _ -> false

(* get the color of a point, if the point is not colored, fail *)
let color_of_point = function 
   (object colored_point) as p -> p#color
 | _ -> failwith "no color in point"

Actually I really think that the essence of object-orientation is
runtime dynamic cast (or class disambiguation or the instanceof Java
operator, etc....). I never happen to use Ocaml object system without
badly needing such a dynamic cast (of course it can be painfully
simulated, etc...).

Perhaps the question has been asked a billion times, then it might go
into a FAQ.


Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
alias: basile<at>tunes<dot>org 
8, rue de la Faïencerie, 92340 Bourg La Reine, France
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