Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the OCaml compiler support true module aliasing?

Tags:

ocaml

I created a new module which is just a shorter alias for a module with a very long name:

module M = ModuleWithLongName

I'm in a situation where the size of the final executable matters. Is the above construct handled sanely by the compiler (i.e. M is really just an alias), or does it replicate the entire contents of ModuleWithLongName inside the module where M is defined?

like image 258
Jon Smark Avatar asked Dec 27 '22 08:12

Jon Smark


2 Answers

No, the OCaml language does not support "true module aliasing".

However, you probably won't notice until you try fairly advanced combination of functors and abstract types. In particular, you can only observe this issue in the type system, not in the runtime behavior of programs: modules are sometimes copied, but mutable states would be aliased between copies (in your example, if ModuleWithLongName.foo is a mutable reference, then M.foo is the same reference).

If you use first-class modules, or define local modules in deeply nested functions, you might observe module copy operations as a non-neglectible cost in the overall computation. The right mental model to reason about performance of first-class modules is that, after type-checking and module-checking, they're exactly records.

like image 136
gasche Avatar answered Feb 11 '23 06:02

gasche


Since version 4.02, OCaml supports true module aliases.

like image 24
ChriS Avatar answered Feb 11 '23 07:02

ChriS