5

OCaml provides mli for interface and a module system.

My question is simple, how to choose from them?

Jackson Tale
  • 25,428
  • 34
  • 149
  • 271

2 Answers2

3

Well, for one, modules are much more powerful. For example, OCaml have Functors (that are like module-level functions). Type this at the interpreter:

module String_set = Set.Make (String)

It creates a module with operations on sets of strings (such as union and intersection).

If you are using only the namespace functionality of modules, creating an explicit module makes it convenient to have a deeper namespace. That is, a module inside a module, like this: Module1.Module2, could be achieved with a file module1.ml that defines a module named Module2.

Of course each file defines a module - file m.ml defines module M - and I find them syntactically convenient (because you don't have an extra indentation...)

Also, an .mli serves a different purpose: interface files are used for documenting the type of top-level definitions, and this including modules themselves. If you define a module X inside a file m.ml, you can put X's signature in m.mli as well. But .mli files are optional, so even if you prefer to define your modules as files, you don't need to create an interface file.

PS: Modules in OCaml are so powerful that a common topic is "should I use modules or objects?". Check also first class modules for added magic.

Community
  • 1
  • 1
darque
  • 1,566
  • 1
  • 14
  • 22
1

It's just two way to access to the same thing

When you create the module Foo in the file Bar.ml, you'll have to access it by Bar.Foo.function . If you create a bar.mli, all functions defined in this file will be accessible syntactically by Bar.function . Others functions are just interns.

Ontologiae
  • 595
  • 4
  • 11