caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] dynamic runtime cast in Ocaml
@ 2002-11-06 11:53 Basile STARYNKEVITCH
  2002-11-06 12:54 ` Xavier Leroy
  0 siblings, 1 reply; 9+ messages in thread
From: Basile STARYNKEVITCH @ 2002-11-06 11:53 UTC (permalink / raw)
  To: caml-list

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
test.

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.

regards.
-- 

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2002-11-12  9:09 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-06 11:53 [Caml-list] dynamic runtime cast in Ocaml Basile STARYNKEVITCH
2002-11-06 12:54 ` Xavier Leroy
2002-11-07  2:01   ` Michael Vanier
2002-11-07  8:11     ` Jacques Garrigue
2002-11-07  3:28   ` Walid Taha
2002-11-07  7:22     ` Jacques Garrigue
2002-11-07 14:49       ` Walid Taha
2002-11-08 15:26       ` Alain Frisch
2002-11-11  9:26         ` Jacques Garrigue

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).