(***********************************************************************) (* *) (* cast objet en O'Caml *) (* *) (* Emmanuel Chailloux, Equipe PPS, universite P7 *) (* *) (* *) (***********************************************************************) (* $Id: test2.ml,v 1.2 2001/08/30 15:40:37 emmanuel Exp $ *) (* test2.ml : fichier de test top / \ / \ left right *) class virtual top = object(self) method virtual to_string : string method print = print_string (self#to_string) method clone = Oo.copy(self) method eq (a : top) = true end;; class left (x : int) = object (self ) subinherit top val l = x method get_l = l method to_string = string_of_int l method eq ( x : top) = let xl = cast x to left in l = (xl#get_l) end;; class right (x : int) = object (self ) subinherit top val l = x method get_l = l method to_string = string_of_int l method eq (x:top) = let xl = cast x to right in l = (xl#get_l) end;; let l = new left 10;; let l2 = new left 10;; let t1 = ( l :> top);; let r = new right 10;; let t2 = ( r :> top);; l#print;; print_newline();; l2#print;; print_newline();; if l#eq(l2 :> top) then print_string "OK" else print_string "PB";; print_newline();; try ignore(l#eq ( r :> top)); print_string "PB" with Cast.Cast_failure _ -> print_string "OK";; print_newline();;