71Fermer73
BookeldOrLe 27/01/2006 à 13:45
❰13:44❱ benjamin@bookeldor-main:~/merdier❯ocaml
        Objective Caml version 3.08.3

# class mother =
object(o)
 method m () =
  print_string "mother\n"
end;;
        class mother : object method m : unit -> unit end

# class daughter =
object(o)
 inherit mother as mom
 method m2 () =
  print_string "daughter\n"
end;;
          class daughter : object method m : unit -> unit method m2 : unit -> unit end

# let plop () =
   let c = new mother
   and d = new daughter
   in
   let e = (d :> mother)
   in
     c#m ();
     d#m2 ();
     (e :> daughter)#m2 ()
  ;;
This expression cannot be coerced to type
  daughter = < m : unit -> unit; m2 : unit -> unit >;
it has type mother but is here used with type #daughter
Only the second object type has a method m2

# let plop_qui_marche () =
 let c = new mother
 and d = new daughter
 in
 let e = (d :> mother)
 in
   c#m ();
   d#m2 ();
   ((Obj.magic e) : daughter)#m2 ()
;;
                  val plop_qui_marche : unit -> unit = <fun>

# plop_qui_marche ();;
mother
daughter
daughter
- : unit = ()