如何在 OCaml 中创建具有多个参数的类型?
我正在尝试创建一个具有多个类型参数的类型。我知道如何使用一个参数创建一种类型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行此操作的方法是:
类型参数不会被柯里化,因此您需要以元组形式将它们作为单个参数提供。 Hashtbl 模块就是一个很好的例子:
The way to do this is:
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) Couple = 'a * 'b ;;
例如...
# type ('a, 'b) couple = 'a * 'b ;;
For instance...