About the primary file organization

in testmodule.ml file

module TestModule = struct type my_type = MyType1 | MyType2 end 

How can I use TestModule at the top level?

after "ocamlc -c testmodule.ml" (this generated testmodule.cmo / cmi)

I tried "open TestModule", but the error "unbound module TestModule" occurred.

  Objective Caml version 3.10.0 # open TestModule;; Unbound module TestModule 

Then I tried to make the top level with this module. but...

 indi@www :~/std/toq$ ocamlmktop -o mytop testmodule.ml indi@www :~/std/toq$ ./mytop Objective Caml version 3.10.0 # TestModule.MyType1;; Unbound constructor TestModule.MyType1 # open TestModule;; Unbound module TestModule 

What can I do to use my TestModule ???

+4
source share
3 answers

The directives you can use for this effect are listed in the manual .

You can try #use "testmodule.ml";; , or, alternatively, #load "testmodule.cmo";; after compiling your module.

+4
source

As mentioned in huitseeker, you can use #use "testmodule.ml";; . However, this will make the Testmodule module available, and your Testmodule module is actually Testmodule.TestModule . .cmo files (created from .ml files) define a module whose name matches the name CMO with a capital letter. So I would omit the module TestModule ... your code and just put your code in a file called testModule.ml . Then you can compile it and use #use "testmodule.ml";; to access the module.

+2
source

Summarize that: several small details all conspired with you :-)

Firstly, as mentioned earlier, you need to load the code into the top layer using #use "testmodule.ml";; for source files or #load "testmodule.cmo";; for compiled files or use ocamlmktop . This explains why your first attempt did not work: you did not download the code.

Secondly, you defined module TestModule in a file called testmodule.ml . Keep in mind that a file defines its own module based on its name. So, in order to access your module, you need to write Testmodule.TestModule . This explains why your second attempt did not work: the code was loaded, but had an unexpected name.

You might want to remove the TestModule definition and rename the file to testmodule.ml (or perhaps the more idiomatic test_module.ml ).

0
source

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


All Articles