如何在Ocaml中包含目录的内容?

huangapple go评论63阅读模式
英文:

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

其中bindune文件如下:

; 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 *)

huangapple
  • 本文由 发表于 2023年6月1日 17:29:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定