# class point : int -> object val mutable x : int method get_x : int method move : int -> unit end # val p : point = # - : int = 7 # - : unit = () # - : int = 10 # val q : point = # - : int * int = (10, 17) # class color_point : int -> string -> object val c : string val mutable x : int method color : string method get_x : int method move : int -> unit end # val p' : color_point = # - : int * string = (5, "red") # val l : point list = [; ] # val get_x : < get_x : 'a; .. > -> 'a = # val set_x : < set_x : 'a; .. > -> 'a = # - : int list = [10; 5] # # class ref x_init = object val mutable x = x_init method get = x method set y = x <- y end;; Error: Some type variables are unbound in this type: class ref : 'a -> object val mutable x : 'a method get : 'a method set : 'a -> unit end The method get has type 'a where 'a is unbound # class ref : int -> object val mutable x : int method get : int method set : int -> unit end # class ['a] ref : 'a -> object val mutable x : 'a method get : 'a method set : 'a -> unit end # - : int = 2 # class ['a] circle : 'a -> object constraint 'a = < move : int -> unit; .. > val mutable center : 'a method center : 'a method move : int -> unit method set_center : 'a -> unit end # class ['a] circle : 'a -> object constraint 'a = #point val mutable center : 'a method center : 'a method move : int -> unit method set_center : 'a -> unit end # val c : point circle = val c' : color_point circle = # class ['a] color_circle : 'a -> object constraint 'a = #color_point val mutable center : 'a method center : 'a method color : string method move : int -> unit method set_center : 'a -> unit end # # let c'' = new color_circle p;; Error: This expression has type point but an expression was expected of type #color_point The first object type has no method color # val c'' : color_point color_circle = # - : color_point circle = # # (c'' :> point circle);; (* Echec *) Error: Type color_point color_circle = < center : color_point; color : string; move : int -> unit; set_center : color_point -> unit > is not a subtype of point circle = < center : point; move : int -> unit; set_center : point -> unit > Type point = point is not a subtype of color_point = color_point # # fun x -> (x : color_point color_circle :> point circle);; Error: Type color_point color_circle = < center : color_point; color : string; move : int -> unit; set_center : color_point -> unit > is not a subtype of point circle = < center : point; move : int -> unit; set_center : point -> unit > Type point = point is not a subtype of color_point = color_point # class printable_point : int -> object val mutable x : int method get_x : int method move : int -> unit method print : unit end # val p : printable_point = # 7- : unit = () # # class printable_color_point y c = object (self) inherit color_point y c inherit printable_point y as super method print = print_string "("; super#print; print_string ", "; print_string (self#color); print_string ")" end;; Warning 13: the following instance variables are overriden by the class printable_point : x The behaviour changed in ocaml 3.10 (previous behaviour was hiding.) class printable_color_point : int -> string -> object val c : string val mutable x : int method color : string method get_x : int method move : int -> unit method print : unit end # val p' : printable_color_point = # (7, red)- : unit = () # class functional_point : int -> object ('a) val x : int method get_x : int method move : int -> 'a end # val p : functional_point = # - : int = 7 # - : int = 10 # - : int = 7 # - : #functional_point -> functional_point = # class virtual ['a] lst : unit -> object method virtual hd : 'a method iter : ('a -> unit) -> unit method map : ('a -> 'a) -> 'a lst method virtual null : bool method print : ('a -> unit) -> unit method virtual tl : 'a lst end and ['a] nil : unit -> object method hd : 'a method iter : ('a -> unit) -> unit method map : ('a -> 'a) -> 'a lst method null : bool method print : ('a -> unit) -> unit method tl : 'a lst end and ['a] cons : 'a -> 'a lst -> object val h : 'a val t : 'a lst method hd : 'a method iter : ('a -> unit) -> unit method map : ('a -> 'a) -> 'a lst method null : bool method print : ('a -> unit) -> unit method tl : 'a lst end # val l1 : int lst = # (3::10::[])- : unit = () # val l2 : int lst = # (4::11::[])- : unit = () # val map_list : ('a -> 'b) -> 'a lst -> 'b lst = # val p1 : printable_color_point lst = # ((3, red)::(10, red)::[])- : unit = () # class virtual comparable : unit -> object ('a) method virtual leq : 'a -> bool end # class int_comparable : int -> object ('a) val x : int method leq : 'a -> bool method x : int end # class int_comparable2 : int -> object ('a) val x : int val mutable x' : int method leq : 'a -> bool method set_x : int -> unit method x : int end # class ['a] sorted_list : unit -> object constraint 'a = #comparable val mutable l : 'a list method add : 'a -> unit method hd : 'a end # val l : _#comparable sorted_list = # val c : int_comparable = # - : unit = () # val c2 : int_comparable2 = # # l#add (c2 :> int_comparable);; (* Echec : 'a comp2 n'est un sous-type *) Error: Type int_comparable2 = < leq : int_comparable2 -> bool; set_x : int -> unit; x : int > is not a subtype of int_comparable = < leq : int_comparable -> bool; x : int > Type int_comparable = < leq : int_comparable -> bool; x : int > is not a subtype of int_comparable2 = < leq : int_comparable2 -> bool; set_x : int -> unit; x : int > # - : unit = () # class int_comparable3 : int -> object val mutable x : int method leq : int_comparable -> bool method setx : int -> unit method x : int end # val c3 : int_comparable3 = # - : unit = () # # (new sorted_list ())#add c3;; (* Echec : leq n'est pas binaire *) Error: This expression has type int_comparable3 = < leq : int_comparable -> bool; setx : int -> unit; x : int > but an expression was expected of type #comparable as 'a = < leq : 'a -> bool; .. > Type int_comparable = < leq : int_comparable -> bool; x : int > is not compatible with type int_comparable3 = < leq : int_comparable -> bool; setx : int -> unit; x : int > The first object type has no method setx # val sort : (#comparable as 'a) list -> 'a list = # # let pr l = List.map (fun c -> print_int c#x; print_string " ") l; print_newline ();; Warning 10: this expression should have type unit. val pr : < x : int; .. > list -> unit = # val l : int_comparable list = [; ; ] # 5 2 4 - : unit = () # 2 4 5 - : unit = () # val l : int_comparable2 list = [; ] # 2 0 - : unit = () # 0 2 - : unit = () # val min : (#comparable as 'a) -> 'a -> 'a = # - : int = 7 # - : int = 3 # class ['a] link : 'a -> object ('b) val mutable next : 'b option val mutable x : 'a method append : 'b option -> unit method next : 'b option method set_next : 'b option -> unit method set_x : 'a -> unit method x : 'a end # class ['a] double_link : 'a -> object ('b) val mutable next : 'b option val mutable prev : 'b option val mutable x : 'a method append : 'b option -> unit method next : 'b option method prev : 'b option method set_next : 'b option -> unit method set_prev : 'b option -> unit method set_x : 'a -> unit method x : 'a end # val fold_right : ('a -> 'b -> 'b) -> 'a #link option -> 'b -> 'b = # class calculator : unit -> object ('a) val mutable acc : float val mutable arg : float val mutable equals : 'a -> float method acc : float method add : 'a method arg : float method enter : float -> 'a method equals : float method sub : 'a end # - : float = 5. # - : float = 1.5 # - : float = 15. # class calculator : unit -> object ('a) val mutable acc : float val mutable arg : float val mutable equals : 'a -> float method acc : float method add : 'a method arg : float method enter : float -> 'a method equals : float method sub : 'a end # - : float = 5. # - : float = 1.5 # - : float = 15. # class calculator : float -> float -> object val acc : float val arg : float method add : calculator method enter : float -> calculator method equals : float method sub : calculator end and calculator_add : float -> float -> object val acc : float val arg : float method add : calculator method enter : float -> calculator method equals : float method sub : calculator end and calculator_sub : float -> float -> object val acc : float val arg : float method add : calculator method enter : float -> calculator method equals : float method sub : calculator end # val calculator : calculator = # - : float = 5. # - : float = 1.5 # - : float = 15. #