2011-11-02 21 views
5

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

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

回答

9

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

(这些各种各样的结构的变得棘手相当快速,以我的经验。)