英文:
What is the purpose of Jane Street Base's *_intf.ml files?
问题
.mli文件通常为其对应的.ml文件提供了“接口”。
但在Jane Street的Base中,有时会有三个具有相同名称但不同扩展名的文件,例如(commit):
- binary_searchable.ml
- binary_searchable.mli
- binary_searchable_intf.ml
为什么只有一些.ml
文件有相应的.ml_intf.ml
文件?而且,更一般地说,.mli
格式的限制是什么,以至于有时需要_intf.ml
文件呢?
英文:
This is a general OCaml question.
A .mli file usually provides the "interface" for its corresponding .ml file.
But in Jane Street's Base sometimes there are three files with the same name-minus-extension, for example (commit):
- binary_searchable.ml
- binary_searchable.mli
- binary_searchable_intf.ml
Why do only some .ml
s have corresponding .ml_intf.ml
files? And, more generally, what are the limitations of the .mli
format such that _intf.ml
files are sometimes needed?
答案1
得分: 4
这在这篇名为“the _intf trick”的文章中有详细解释,但简而言之,据我理解,它的目的是避免在接口文件和实现文件中重复大型类型定义,特别是模块类型。
基本上,如果你有一个大型类型定义,不打算在外部抽象掉它,比如:
type t =
| A of int
| B of string
...
| Z of (int array, string * float)
你可以将它放在Foo_intf.ml
中,然后在.ml
和.mli
文件中简单地写成:
include Foo_intf
英文:
This is explained in detail in this article on "the _intf trick", but in short, as I understand it, it's to avoid having to repeat large type definitions, especially module types, in both the interface and implementation files.
Essentially, if you have a large type definition that is not intended to be abstracted away externally, say:
type t =
| A of int
| B of string
...
| Z of (int array, string * float)
Instead of duplicating this definition in both Foo.ml
and Foo.mli
, you can put it in Foo_intf.ml
, and then in the .ml
and .mli
file simply write:
include Foo_intf
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论