Advertisement
jvanbure

Module subtyping

Oct 24th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.60 KB | None | 0 0
  1. module type Big = sig
  2.   type t
  3.  
  4.   val v : t
  5. end
  6.  
  7. module type Small = sig
  8.   include Big
  9.  
  10.   val v' : t
  11. end
  12.  
  13. let get_v (type a) (smol : (module Big with type t = a)) : a =
  14.   let module M = (val smol) in
  15.   M.v
  16. ;;
  17.  
  18. let create () =
  19.   ( module struct
  20.     type t = int
  21.  
  22.     let v = 0
  23.     let v' = 1
  24.   end
  25.   : Small
  26.     with type t = int )
  27. ;;
  28.  
  29. module Sub' : Small = (val create ())
  30.  
  31. let _zero = get_v (module Sub')
  32.  
  33. let _zero =
  34.   let module M = (val create ()) in
  35.   get_v (module M)
  36. ;;
  37.  
  38. (** Why does the above work, but not this?: *)
  39. let _zero = get_v (create ())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement