Domanda

Say I have 2 functors that look something like:

module X (T : sig type t end) = struct
  type t = T.t
  ...
end

module Y (T : sig type t end) = struct
  type t = T.t
  ...
end

And I'd like to combine their result in a single module:

module M = struct
  include X(struct type t = int end)
  include Y(struct type t = int end)
end

Of course this doesn't work because I'm defining t twice inside of M. I know one way would be not to specify t inside of X or Y but I can't do that since then I can't write the signature that X or Y would produce because it involves t. What I wish I had is a way of somehow getting the signature of X or Y without the t.

EDIT: To make my question less abstract I'll give more context: I'm trying to unify in Request.S and Response.S in cohttp:

https://github.com/mirage/ocaml-cohttp/blob/master/cohttp/request.mli

https://github.com/mirage/ocaml-cohttp/blob/master/cohttp/response.mli

And here's where the module is combined:

https://github.com/mirage/ocaml-cohttp/blob/master/lwt/cohttp_lwt.ml#L36

È stato utile?

Soluzione

You can use := to remove t from Y(T)'s signature:

module X (T : sig type t end) = struct
  type t = T.t
end

module Y (T : sig type t end) = struct
  type t = T.t
end

module M = struct
  module T = struct type t = int end
  include X(T)
  include (Y(T) : module type of Y(T) with type t := T.t)
end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top