英文:
How to include contents of a directory in Ocaml?
问题
如何在Ocaml中从目录中包含文件的内容/模块?
在这个示例中,如何在file_1中访问file_2的内容。
我无法弄清楚,尽管它似乎是微不足道的。
英文:
How do you include a file's content / module from a directory in Ocaml ?
└── bin
├── file_1.ml
└── directory
└── file_2.ml
In this example, how do you access file_2 contents in file_1.
I can't figure it out, even though it seems trivial.
答案1
得分: 3
你可以使用include_subdirs
来告诉Dune,你希望包括子目录:
└── bin
├── dune
├── file_1.ml
└── directory
└── file_2.ml
有两种不同的包含模式,不限定的包含:
; bin/dune file
(include_subdirs unqualified)
(executable (name file_1))
这将使子目录中的所有模块在主目录命名空间中可用:
(* bin/file_1.ml *)
module F2 = File_2
或者有限定的包含:
; bin/dune file
(include_subdirs qualified)
(executable (name file_1))
这将使子目录中的模块以路径a/b/c
的方式可用,例如A.B.C.File_name
:
(* bin/file_1.ml *)
module F2 = Directory.File_2
另一种将目录内容绑定到Dune的方式是将目录设置为本地库:
└── bin
├── dune
├── file_1.ml
└── local_lib_dir
├── dune
└── file_2.ml
其中bin
的dune
文件如下:
; bin/dune
(executable
(name file_1)
(libraries local_lib)
)
而本地目录中的dune
文件如下:
; bin/local_lib_dir/dune
(library
(name local_lib)
)
然后,你可以在bin
目录中使用file_2
定义的模块:
(* bin/file_1.ml *)
module F2 = Local_dir.File_2
英文:
You can use include_subdirs
to signal to dune that you wish to include subdirectories:
└── bin
├── dune
├── file_1.ml
└── directory
└── file_2.ml
There two different mode of inclusion, unqualified inclusion
; bin/dune file
(include_subdirs unqualified)
(executable (name file_1))
that make all modules from subdirectories available in the main directory namespace:
(* bin/file_1.ml *)
module F2 = File_2
or qualified inclusion
; bin/dune file
(include_subdirs qualified)
(executable (name file_1))
that makes modules from subdirectory at path a/b/c
available as A.B.C.File_name
:
(* bin/file_1.ml *)
module F2 = Directory.File_2
Another possibility to bind directory contents with dune is to make the directory a local library:
└── bin
├── dune
├── file_1.ml
└── local_lib_dir
├── dune
└── file_2.ml
where the dune
file for bin
is
; bin/dune
(executable
(name file_1)
(libraries local_lib)
)
and the one in the local directory is
; bin/local_lib_dir/dune
(library
(name local_lib)
)
Then one can use the module defined by file_2
with
(* bin/file_1.ml *)
module F2 = Local_dir.File_2
inside the bin
directory.
答案2
得分: -1
(* file_2.ml *)
module File2 = struct
(* 在这里定义你的模块内容 *)
end
(* file_1.ml *)
open Directory.File2 (* 假设 "Directory" 是父目录的名称 *)
(* 访问模块的内容 *)
英文:
(* file_2.ml *)
module File2 = struct
(* Define your module contents here *)
end
(* file_1.ml *)
open Directory.File2 (* Assuming "Directory" is the name of the parent directory *)
(* Access the contents of the module *)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论