about basic file organization
in file testmodule.ml
module TestModule =
struct
type my_type = MyType1 | MyType2
end
How can I use TestModule in top-level?
after "ocamlc -c testmodule.ml" (this generated testmodule.cmo/cmi)
I tried "open TestModule", but开发者_运维问答 error "unbound module TestModule" occured.
Objective Caml version 3.10.0
# open TestModule;;
Unbound module TestModule
then, I tried making top-level with that 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 for using my TestModule???
The directives you can use in the toplevel to that effect are listed in the manual.
You can try #use "testmodule.ml";;
, or, alternatively #load "testmodule.cmo";;
after having compiled your module.
As huitseeker mentioned, you can use #use "testmodule.ml";;
. However, this will make the module Testmodule
avialable, and your TestModule
module is actually Testmodule.TestModule
. .cmo
files (generated from .ml
files) define a module whose name is that of the CMO with the first letter capitalized. Therefore, I would omit the module TestModule ...
part of your code, and simply put your code in a file called testModule.ml
. You can then compile it and use #use "testModule.ml";;
to access the module.
Tu summarize: a few minor details all conspired together against you :-)
First, as mentioned previously, you need to load the code into the toplevel with either #use "testmodule.ml";;
for source files or #load "testmodule.cmo";;
for compiled files, or to use ocamlmktop
. This explains why your first attempt did not work: you did not load the code.
Second, you defined module TestModule
in a file called testmodule.ml
. Keep in mind that the 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 wish to remove the TestModule
definition and rename the file to testModule.ml
(or the perhaps more idiomatic test_module.ml
).
精彩评论