如何在 OCaml 中创建具有多个参数的类型?

发布于 2024-08-13 06:17:44 字数 136 浏览 2 评论 0原文

我正在尝试创建一个具有多个类型参数的类型。我知道如何使用一个参数创建一种类型:

type 'a foo = 'a * int

但我需要有两个参数,以便我可以参数化“int”部分。我该怎么做?

I'm trying to create a type that has multiple type parameters. I know how to make a type with one parameter:

type 'a foo = 'a * int

But I need to have two parameters, so that I can parameterize the 'int' part. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

み青杉依旧 2024-08-20 06:17:44

执行此操作的方法是:

type ('a, 'b) foo = 'a * 'b

类型参数不会被柯里化,因此您需要以元组形式将它们作为单个参数提供。 Hashtbl 模块就是一个很好的例子:

类型 ('a, 'b) t 

来自类型“a”的哈希表的类型
输入“b”。

The way to do this is:

type ('a, 'b) foo = 'a * 'b

Type parameters aren't curried, so you need to provide them in tuple form as a single parameter. A good example of this is the Hashtbl module:

type ('a, 'b) t 

The type of hash tables from type 'a
to type 'b.

你的笑 2024-08-20 06:17:44

# type ('a, 'b) Couple = 'a * 'b ;;

例如...

# type ('a, 'b) couple = 'a * 'b ;;

For instance...

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