(***********************************************************************) (* *) (* cast objet en O'Caml *) (* *) (* Emmanuel Chailloux, Equipe PPS, universite P7 *) (* *) (* *) (***********************************************************************) (* $Id: test3.ml,v 1.2 2001/08/30 15:40:37 emmanuel Exp $ *) (* test3.ml : fichier de test t / | \ / | \ a b c \ | / \ | / abc *) class t = object end;; class a (x:int) = object inherit t val a = x method get_a = a end;; class b (x:int) = object inherit t val b = x method get_b = b end;; class c (x:int) = object inherit t val c = x method get_c = c end;; class abc x = object(self) inherit a x inherit b x inherit c x method get_abc = (self#get_a), (self#get_b), (self#get_c) end;; let abc1 = new abc 1;; let a1 = (upcast abc1 from abc to a);; let b1 = (upcast abc1 from abc to b);; let c1 = (upcast abc1 from abc to c);; downcast a1 from a to abc;; downcast b1 from b to abc;; downcast c1 from c to abc;; let t1 = (upcast a1 from a to t);; let t2 = (upcast b1 from b to t);; let t3 = (upcast c1 from c to t);; downcast t1 from t to a;; downcast t1 from t to b;; downcast t1 from t to c;; let r = downcast t1 from t to abc;; r#get_abc;; print_string "OK";; print_newline();; try ignore (upcast(downcast b1 from b to abc) from abc to c); "OK" with _ -> "PB";;