我想定义一种记录类型,其中一个成员为数组类型,如下所示:
type h = {x:int; y:int; pic : Array of int};;
这会在 of
位置产生语法错误:
type h = {x:int; y:int; pic : Array *of* int};;
现在,如果我使用中间类型来命名 int 数组
,它就可以工作:
type a = Array of int;;
type h = {x:int; y:int; pic : a};;
这是一个缺陷还是我错过了什么? (我的ocaml版本是4.05.0)
然后,一旦定义了 type h
我就无法使用它:
let n = {x=0;y=0;pic=[| 0 |]};;
我收到错误:
Error: This expression has type 'a array but an expression was expected of type
a
最佳答案
int
数组的类型是 int array
。 type a = Array of int
定义了一个具有单个构造函数 Array
的变体,该构造函数采用 int
类型的有效负载。
关于arrays - 记录类型中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47857321/