/experimental/garrigue/objvariant.ml

http://github.com/multani/ocaml-mirror · OCaml · 42 lines · 31 code · 10 blank · 1 comment · 0 complexity · a96e987b69ba2175acc5f27e3188f24f MD5 · raw file

  1. (* use with [cvs update -r objvariants typing] *)
  2. let f (x : [> ]) = x#m 3;;
  3. let o = object method m x = x+2 end;;
  4. f (`A o);;
  5. let l = [`A o; `B(object method m x = x -2 method y = 3 end)];;
  6. List.map f l;;
  7. let g = function `A x -> x#m 3 | `B x -> x#y;;
  8. List.map g l;;
  9. fun x -> ignore (x=f); List.map x l;;
  10. fun (x : [< `A of _ | `B of _] -> int) -> ignore (x=f); List.map x l;;
  11. class cvar name =
  12. object
  13. method name = name
  14. method print ppf = Format.pp_print_string ppf name
  15. end
  16. type var = [`Var of cvar]
  17. class cint n =
  18. object
  19. method n = n
  20. method print ppf = Format.pp_print_int ppf n
  21. end
  22. class ['a] cadd (e1 : 'a) (e2 : 'a) =
  23. object
  24. constraint 'a = [> ]
  25. method e1 = e1
  26. method e2 = e2
  27. method print ppf = Format.fprintf ppf "(%t, %t)" e1#print e2#print
  28. end
  29. type 'a expr = [var | `Int of cint | `Add of 'a cadd]
  30. type expr1 = expr1 expr
  31. let print = Format.printf "%t@."
  32. let e1 : expr1 = `Add (new cadd (`Var (new cvar "x")) (`Int (new cint 2)))