Jane Street Base的*_intf.ml文件的目的是什么?

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

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 .mls 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

huangapple
  • 本文由 发表于 2023年2月19日 00:18:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494667.html
匿名

发表评论

匿名网友

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

确定