Reputation: 3919
I'm running Ubuntu, installed Ocaml, and wrote the following script, as I found it in a set of instructions (course notes):
;; open Assert
;; print_int 1
saved this file as test.ml
. Then in a terminal I navigated to the folder containing the file and executed
$ ocaml test.ml
The containing folder has the assert.ml
file and assert.mli
. I looked at some documentation in these course notes and on Ocaml's website, and can't find any official statement about how to open a module other than trying the above. When I run this, I get the error message
File "test.ml", line 1, characters 8-14:
Error: Unbound module Assert
Can anyone describe how this is supposed to work?
Upvotes: 2
Views: 7902
Reputation: 66793
If you have just assert.ml and assert.mli, then you need to compile them first before they can be used in other code.
You can use the ocaml compiler directly like this:
$ ocamlc -c assert.mli
$ ocamlc -c assert.ml
This will create files named assert.cmi (the compiled version of assert.ml) and assert.cmo (the compiled version of assert.ml).
After that, your test.ml file should work OK if you run it like this:
$ ocaml assert.cmo test.ml
(Thanks @camlspotter.)
The open
construct in OCaml doesn't cause a module to become available if it wasn't available before. What it does is make the names in the module available directly. Without open
, you need to prefix the names with the name of the module: Module.name
. In my opinion (shared by some others) it is best to limit the use of open
, to avoid introducing too many names into the scope of your code.
As a side comment, it is stylistically very strange to begin your lines with ;;
. This token is used to tell the OCaml toplevel (the interpreter) that it should evaluate what you've typed so far. So it usually comes after some interesting expression.
I personally don't use ;;
at all in source files. I only use it when typing expressions into the toplevel.
Upvotes: 6