匿名OCAML结构是否有任何用例
OCAML手册,第2章说
结构,由结构…结构构造引入,其中包含任意定义顺序。该结构通常在模块绑定中给出一个名称。
是否有任何可用于创建结构的用例,而不是给它一个模块名称。 如果不是,那么我们总是使用
module Name =
struct
...
end
,因此结构关键字似乎有点冗余。
The OCaml manual, Chapter 2, says
a structure and is introduced by the struct…end construct, which contains an arbitrary sequence of definitions. The structure is usually given a name with the module binding.
Is there any use case for the creating a struct and not giving it a module name.
If not, then we always use
module Name =
struct
...
end
and so the struct keyword seems a bit redundant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用无名结构是可能甚至常见的(至少在我的代码中)。一个例子:
It's possible and even common (in my code at least) to use nameless structures. One example:
为了稍微扩展Jeffrey的答案,OCAML函子将一个模块映射到另一个模块。它不在乎模块的名称。
考虑以下微不足道的例子。
我已经定义了一个函数 a ,该模块
b
可满足模块类型sig
。现在,我可以定义一个模块twenty_one
提供x
21
的值,然后将其提供给函数a 创建模块
c
。或者我可以直接使用匿名结构。
我们甚至不需要 name
sig
。强烈地进入意见领域,但您可能希望在您的代码中提供这些内容,如果它有助于重复使用和表达。
例如
VS。
To expand slightly on Jeffrey's answer, an OCaml functor maps a module to another module. It doesn't care about the module's name.
Consider the following trivial example.
I've defined a functor
A
which takes a moduleB
that fulfills the module typeSIG
. Now, I could define a moduleTwenty_one
that supplies anx
value of21
, and then give that to the functorA
to create moduleC
.Or I could directly use an anonymous structure.
We don't even need to name
SIG
.Strongly into opinion territory, but you may want to give these things names in your code, if it aids with reuse and expressivess.
E.g.
Vs.
您可以使用匿名结构可以做的另一件事,这与最近(截至4.08.0)OCAML更相关,是与它使用
open
的能力,用于一种语法缩短的方式来隐藏名称您的命名空间(直到您编写一个接口文件):此功能称为通用开放,并且相关的手册页为在这里。
Another thing you can do with an anonymous structure, that's more relevant to recent (as of 4.08.0) OCaml, is the ability to use
open
with it, for a syntactically-cheap way to hide names in your namespace (until you write an interface file anyway):This feature is called generalized-open, and the relevant manual page for it is here.