spacepaste

  1.  
  2. class type num =
  3. object
  4. method get_val : float
  5. method add : num -> unit
  6. method parse : string -> unit
  7. end
  8. class int =
  9. object
  10. val mutable x = 1
  11. method get_val = float x
  12. method add : num -> unit = fun o -> x <- (int_of_float o#get_val) + x
  13. method parse s = x <- int_of_string s
  14. end
  15. class float =
  16. object
  17. val mutable x = 1.
  18. method get_val = x
  19. method add : num -> unit = fun o -> x <- o#get_val +. x
  20. method parse s = x <- float_of_string s
  21. end
  22. let make_obj = function
  23. | "float" -> (new float :> num)
  24. | "int" -> (new int :> num)
  25. | _ -> invalid_arg "which module"
  26. let parse s = make_obj s
  27. let do_repetitive_work x =
  28. let y = Oo.copy x in
  29. y#add x;
  30. y#add (Oo.copy x);
  31. y
  32. let () =
  33. let x = parse Sys.argv.(1) in
  34. Printf.printf "%g\n" (do_repetitive_work x)#get_val
  35.