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 = 0
  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 = 0.
  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 parse s =
  23. let ret = if String.contains s '.' then (new float :> num) else (new int :> num) in
  24. ret#parse s;
  25. ret
  26.