I’m trying to make independent functors that take a module as argument and return a similar module with a new value. I’d like to be able to chain such functors in a way that keeps the contribution of each one without having to define signatures for each possible combination.
I guess that could also be seen as a kind of polymorphism for module signatures. Anyway, here’s an example to clarify what I mean:
module type X = sig val x : int end module Extend_with_y(M : X) = struct include M let y = 2 end module Extend_with_z(M : X) = struct include M let z = 3 end module A = struct let x = 1 end module B = Extend_with_y(A) module C = Extend_with_z(B) let () = Printf.printf "%d, %d, %d\n" C.x C.y C.z
Currently, this results in:
File "ocaml_sig_param.ml", line 25, characters 35-38: 25 | Printf.printf "%d, %d, %d\n" C.x C.y C.z ^^^ Error: Unbound value C.y
I would like to make it work by keeping
Extend_with_z would have a signature like
(M : sig include X + other things end) -> sig include X val z : int + those other things end, so that it reexports what other types or values of
M instead of erasing them and only keeping
Is it possible? Would it even make sense for OCaml to provide such a behavior? It’s possible that I’m missing something completely obvious, since I haven’t really used functors extensively during the last few months.