小编ZJa*_*nes的帖子

在模块外部使用开放式联合,它们被定义为

为什么这样可以正常工作?

module Account = struct

 type account_type = Current of float | Savings of float

end

let sarah = Account.Current 100.0;;
Run Code Online (Sandbox Code Playgroud)

虽然下面的最后一行产生了Error: syntax error

module Account = struct

  type 'a account_type = [> `Current of float | `Savings of float ] as 'a

end

let pete = Account.`Current 100.0;;
Run Code Online (Sandbox Code Playgroud)

也就是说,为什么我不能在不打开模块的情况下在模块外部使用open union类型?我应该说我发现将最后一行更改为:

open Account;;
let pete = `Current 100.0;;
Run Code Online (Sandbox Code Playgroud)

工作正常,但显然这很麻烦,如果我account_type经常使用,或者我必须Account在使用的任何代码部分的开头打开account_type,这意味着我牺牲了我通过使用签名获得的抽象Account
我'已经浏览了几个OCaml教程以及INRIA文档,我找不到你如何做到这一点.
是否有可能避免每次我想使用时都打开模块account_type

提前致谢,

扎克

ocaml types variant

5
推荐指数
2
解决办法
114
查看次数

使用开放联合键入定义

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

type 'a choice = [> `One | `Other ] as 'a
Run Code Online (Sandbox Code Playgroud)

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

type choice_list = choice list
Run Code Online (Sandbox Code Playgroud)

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

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

val choice_handler : choice list -> int
Run Code Online (Sandbox Code Playgroud)

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

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

ocaml types variant

5
推荐指数
1
解决办法
297
查看次数

标签 统计

ocaml ×2

types ×2

variant ×2