On 11/02/2011 12:41 PM, Anthony Tavener wrote:
> I've been struggling with this occasionally...
> 
> I'm using nested modules to "open" access to select features of a
> module. My problem is I can't find a way to *expose* types in the parent
> module through such nested modules.
> 
> A simplified example of what I'm looking at:
> 
>   module Vec = struct
> 
>     type t = { x: int; y: int }
>     let make x y = {x;y}
>     let add a b = {x=a.x+b.x; y=a.y+b.y}
> 
>     module Type =
>       (* something which has type t = Vec.t,
>        * with exposed structure when "open"ed.
>        * Also note that Vec is not really an
>        * explicit module like this; instead it
>        * is implemented in vec.ml <http://vec.ml> *)
>   end
> 
> Example usage...
> 
>   let n = Vec.make 2 5
>   open Vec.Type
>   let m = {x=1;y=2}
>   Vec.add m n

I hope I understand the problem correctly.

In order for that code to work, you can do this:

module Vec = struct

  type t = { x: int; y: int }
  let make x y = {x;y}
  let add a b = {x=a.x+b.x; y=a.y+b.y}

  module Type = struct
    type t' = t = { x: int; y: int }
    type t = t' = { x: int; y: int }
    (* something which has type t = Vec.t,
     * with exposed structure when "open"ed.
     * Also note that Vec is not really an
     * explicit module like this; instead it
     * is implemented in vec.ml <http://vec.ml> *)
  end
end


Or more simply:


module Vec = struct

  module Type = struct
    type t = { x: int; y: int }
    (* something which has type t = Vec.t,
     * with exposed structure when "open"ed.
     * Also note that Vec is not really an
     * explicit module like this; instead it
     * is implemented in vec.ml <http://vec.ml> *)
  end

  type t = Type.t = { x: int; y: int }
  let make x y = {x;y}
  let add a b = {x=a.x+b.x; y=a.y+b.y}

end


Now you can open either Vec or Vec.Type and have direct access to the
record fields.


Martin

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to