Is there any way to "depend" on an interface file from a dependency?

It’s weird title framing and I apologise, let me express with more words:

The context here is I want to make sure I’m implementing the same interface as a dependency and would love to depend (or copy/sync/whatever method) on the mli file either given by the library or the cmi itself. This way I will be sure that my implementation type-checks to it.

The only “rule” is that I don’t want to change the library core for this particular case.

I tried copying the cmi file over, but with no much success. Do you have any other idea or solid methodology here?

Thanks :smiley:

Can’t you solve that by having a test ?

Let’s suppose you want to make sure your Myunit module implement Stdlib.Unit. Then you can have a test like:

module type S = module type of Stdlib.Unit

let () = ignore (module Myunit : S)

I can’t do it so easily since the original module is using (modes melange) while mine is using native or bytecode mode.

Can you use module type of? E.g. if the dependency has a file foo.mli, you could use module type of Foo to get its module type. Suppose your implementation is in impl.ml, then your impl.mli would be include module type of Foo.

There’s no possibility to mix mode melange and native modules, unless I have more than one mode on each library which is not possible due to platform specific stuff.

and, If there’s a way of doing so, there’s no way I can use 2 modules with the same name (to type of eachother)

You have foo/a.ml, and bar/a.ml, bar/a.mli, and you want to compile foo/a.ml using bar/a.mli?

Starting with v5 you can pass --cmi-file in case it is in some other directory that the .ml file. Otherwise, the compiler will look for the mli file, not the cmi file, when deciding if it needs to generate a cmi file.

2 Likes

That’s super helpful.

Endup using it with a custom rule:

(rule
 (target interface.mli)
 (action
  (with-stdout-to
   %{target}
   (run
    ocamlc
    -cmi-file
    %{workspace_root}/_opam/lib/library/melange/package.cmi))))
1 Like