具有开放联合的类型定义

发布于 2024-12-13 05:21:05 字数 519 浏览 2 评论 0原文

1)我有一个开放联合定义如下:

type 'a choice = [> `One | `Other ] as 'a

然后我尝试定义一个类型 choice_list:

type choice_list = choice list

这不起作用。如何定义其中一个或多个组件是开联合的类型?

2)如果我放弃创建 choice_list 类型,而只使用 选择列表,当我尝试使用选择列表编写接口/签名语句时,

val choice_handler : choice list -> int

编译器会抱怨type 'a choice = 'aconstraint 'a = [>; `一| `Other] 不包含在感染状态类型中。他们有不同的性质

我的问题是,如何在接口/签名中编写选择列表的类型声明。

1) I have an open union defined as follows:

type 'a choice = [> `One | `Other ] as 'a

I then try to define a type choice_list:

type choice_list = choice list

which does not work. How does one define types where one or more of the components are open unions?

2) If instead I forgo creating the choice_list type, and just use a choice list, when I try writing an interface/signature statement using a choice list,

val choice_handler : choice list -> int

the compiler complains that type 'a choice = 'a constraint 'a = [> `One | `Other ] is not included in type infection_state. They have different arities.

My question is, how does one write the type declaration of choice list in the interface/signature.

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

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

发布评论

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

评论(1

我三岁 2024-12-20 05:21:05

编译器试图告诉您 choice 是一个参数化类型。在类型级别,它的元数为 1。换句话说,您需要提供一个类型参数。您已将参数限制为 [`One|`Other] 的子类型,但除此之外它可以是任何类型:

# ([`One; `Third] : 'a choice list);;
- : [> `One | `Other | `Third ] choice list = [`One; `Third]

如果您想定义选项列表,则额外类型必须来自某处。即,它必须是新类型的参数:(

# type 'a choice_list = 'a choice list;;
type 'a choice_list = 'a choice list constraint 'a = [> `One | `Other ]

根据我的经验,此类构造很快就会变得棘手。)

The compiler is trying to tell you that choice is a parameterized type. At the type level, it has an arity of 1. In other words, you need to supply one type parameter. You have constrained the parameter to be a subtype of [`One|`Other], but other than that it can be any type:

# ([`One; `Third] : 'a choice list);;
- : [> `One | `Other | `Third ] choice list = [`One; `Third]

If you want to define a list of choices, the extra type has to come from somewhere. I.e., it has to be a parameter to the new type:

# type 'a choice_list = 'a choice list;;
type 'a choice_list = 'a choice list constraint 'a = [> `One | `Other ]

(These sorts of constructions get tricky pretty fast, in my experience.)

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