(***********************************************************************) (* *) (* 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 subinherit t val a = x method get_a = a end;; class b (x:int) = object subinherit t val b = x method get_b = b end;; class c (x:int) = object subinherit t val c = x method get_c = c end;; class abc x = object(self) subinherit a x subinherit b x subinherit c x method get_abc = (self#get_a), (self#get_b), (self#get_c) end;; let abc1 = new abc 1;; let a1 = (abc1 :> a);; let b1 = (abc1 :> b);; let c1 = (abc1 :> c);; cast a1 to abc;; cast b1 to abc;; cast c1 to abc;; let t1 = (a1 :> t);; let t2 = (b1 :> t);; let t3 = (c1 :> t);; cast t1 to a;; cast t1 to b;; cast t1 to c;; let r = cast t1 to abc;; r#get_abc;; print_string "OK";; print_newline();; try ignore ( (cast b1 to abc) :> c); "OK" with _ -> "PB";;