-
- class type num =
- object
- method get_val : float
- method add : num -> unit
- method parse : string -> unit
- end
-
- class int =
- object
- val mutable x = 1
- method get_val = float x
- method add : num -> unit = fun o -> x <- (int_of_float o#get_val) + x
- method parse s = x <- int_of_string s
- end
-
- class float =
- object
- val mutable x = 1.
- method get_val = x
- method add : num -> unit = fun o -> x <- o#get_val +. x
- method parse s = x <- float_of_string s
- end
-
- let make_obj = function
- | "float" -> (new float :> num)
- | "int" -> (new int :> num)
- | _ -> invalid_arg "which module"
-
- let parse s = make_obj s
-
- let do_repetitive_work x =
- let y = Oo.copy x in
- y#add x;
- y#add (Oo.copy x);
- y
-
- let () =
- let x = parse Sys.argv.(1) in
- Printf.printf "%g\n" (do_repetitive_work x)#get_val
-