spacepaste

  1.  
  2. open Batteries
  3. class type ['a] number = object ('b)
  4. method value: 'a
  5. method plus: 'b -> 'b
  6. end
  7. class int_wrap (i: int) = object
  8. val i = i
  9. method value = i
  10. method plus (other: int number) = {< i = i + other#value >}
  11. end
  12. class float_wrap (f: float) = object
  13. val f = f
  14. method value = f
  15. method plus (other: float number) = {< f = f +. other#value >}
  16. end
  17. let parse: string -> 'a number = fun s ->
  18. if String.exists s "." then
  19. float_of_string s |> new float_wrap
  20. else
  21. int_of_string s |> new int_wrap
  22.