arrays - 记录类型中的数组

标签 arrays ocaml record

我想定义一种记录类型,其中一个成员为数组类型,如下所示:

 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 arraytype a = Array of int 定义了一个具有单个构造函数 Array 的变体,该构造函数采用 int 类型的有效负载。

关于arrays - 记录类型中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47857321/

相关文章:

audio - 关于录音采样率

c# - 算术运算导致溢出错误 - 对于 c# 中的 byte[] 数组

ios - 移动数组中的所有对象 - IOS 和 Sprite Kit

javascript - 使用元素之一作为引用点从 Javascript 数组中选择元素

makefile - 强制 make 不重新排序自动变量扩展中的先决条件

plot - 绘制图形时像素与记录的关系

javascript - 什么时候数组索引与-1比较

list - OCaml boolean 表达式[[]] == [[]]

printing - 如何在 Ocaml 中打印以另一种类型包装的内容的字符串值?

generics - 使用记录 Haskell 进行泛型派生