Functors in separate files in OCaml?

I want to have a big functor Hello (Blah: Blah_type) and save it in the hello.ml file, but how to do it?

If I were only in my top-level file, I would have the module Hello (Blah: Blah_type) = structure val x = 2 end

but how to put an argument in hello.ml? I canโ€™t just have the whole file "val x = 2" ...?

+6
source share
3 answers

It's impossible. Source files are always represented as regular modules, not functors. This is trivially permitted with one additional open.

+4
source

OCamlPro has a compiler patch and an external tool that can support this:

http://www.ocamlpro.com/blog/2011/08/10/ocaml-pack-functors.html

As far as I know, the official release of the compiler does not support .ml files as functors.

+6
source

To supplement ygrek with a response with a sample of real code instead of a foo.ml file with content

 module type S = sig (* ... *) end module Hello (M : S) = struct (* ... *) end module M : S = struct (* ... *) end module H = Hello(M) (* ... *) 

You may have hello.ml with content

 module type S = sig (* ... *) end module Make (M : S) = struct (* ... *) end 

and foo.ml rewritten as

 module M : Hello.S = struct (* ... *) end module H = Hello.Make(M) (* ... *) 

PS: If you think this is confusing, compacting the M : S or M : Hello.S is optional (M will be forced to apply to this signature when it is passed to the functor), it was just to show how this can be done.

+3
source

Source: https://habr.com/ru/post/912951/


All Articles