匿名OCAML结构是否有任何用例

发布于 2025-02-12 17:50:27 字数 232 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清君侧 2025-02-19 17:50:27

使用无名结构是可能甚至常见的(至少在我的代码中)。一个例子:

module MyStrSet =
   Set.Make(struct type t = string let compare a b = compare b a end)

It's possible and even common (in my code at least) to use nameless structures. One example:

module MyStrSet =
   Set.Make(struct type t = string let compare a b = compare b a end)
独行侠 2025-02-19 17:50:27

为了稍微扩展Jeffrey的答案,OCAML函子将一个模块映射到另一个模块。它不在乎模块的名称

考虑以下微不足道的例子。

module type SIG =
sig
  val x : int
end

module A (B : SIG) =
struct
  let y = B.x * 2
end

我已经定义了一个函数 a ,该模块b可满足模块类型sig。现在,我可以定义一个模块twenty_one提供x 21的值,然后将其提供给函数a 创建模块c

module Twenty_one = 
struct
  let x = 21
end

module C = A (Twenty_one)

或者我可以直接使用匿名结构。

module C = A (struct let x = 21 end)

我们甚至不需要 name sig

module A (B : sig val x : int end) =
struct
  let y = B.x * 2
end

module C = A (struct let x = 21 end)

强烈地进入意见领域,但您可能希望在您的代码中提供这些内容,如果它有助于重复使用和表达。

例如

module Int =
struct
  type t = int
  let compare = compare
end

module Int_map = Map.Make (Int)

VS。

module Int_map = Map.Make (struct type t = int let compare = compare end)

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.

module type SIG =
sig
  val x : int
end

module A (B : SIG) =
struct
  let y = B.x * 2
end

I've defined a functor A which takes a module B that fulfills the module type SIG. Now, I could define a module Twenty_one that supplies an x value of 21, and then give that to the functor A to create module C.

module Twenty_one = 
struct
  let x = 21
end

module C = A (Twenty_one)

Or I could directly use an anonymous structure.

module C = A (struct let x = 21 end)

We don't even need to name SIG.

module A (B : sig val x : int end) =
struct
  let y = B.x * 2
end

module C = A (struct let x = 21 end)

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.

module Int =
struct
  type t = int
  let compare = compare
end

module Int_map = Map.Make (Int)

Vs.

module Int_map = Map.Make (struct type t = int let compare = compare end)
离不开的别离 2025-02-19 17:50:27

您可以使用匿名结构可以做的另一件事,这与最近(截至4.08.0)OCAML更相关,是与它使用open的能力,用于一种语法缩短的方式来隐藏名称您的命名空间(直到您编写一个接口文件):

open struct
  type hidden_type = string
  let hidden_name = 42
end

此功能称为通用开放,并且相关的手册页为在这里

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):

open struct
  type hidden_type = string
  let hidden_name = 42
end

This feature is called generalized-open, and the relevant manual page for it is here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文