【问题标题】:Type definitions with open unions具有开放联合的类型定义
【发布时间】:2011-11-02 14:37:25
【问题描述】:

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

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

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

type choice_list = choice list

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

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

val choice_handler : choice list -> int

编译器抱怨type 'a choice = 'a constraint 'a = [> `One | `Other ] is not included in type infection_state. They have different arities

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

【问题讨论】:

    标签: types ocaml variant


    【解决方案1】:

    编译器试图告诉你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 ]
    

    (根据我的经验,这类结构很快就会变得很棘手。)

    【讨论】:

      猜你喜欢
      • 2023-01-31
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 2011-07-05
      • 2014-08-14
      • 2022-12-18
      • 2019-10-11
      • 2011-03-31
      相关资源
      最近更新 更多