如何在OCaml中定义未初始化的数组?OCaml中未初始化的数组
我尝试这样做是这样的:
let arr = Array.make_matrix 2 2
Error: The type of this expression, '_a -> '_a array array,
contains type variables that cannot be generalized
我当然可以通过自动补0矩阵但后来我有一个类型的问题,因为我想在这个阵列输入和输出通道存储(IC和oc)两个不同的用户:
let fd, _ = conn in
let ic = Lwt_io.of_fd Lwt_io.Input fd in
let oc = Lwt_io.of_fd Lwt_io.Output fd in
请告诉我,我该如何填充这样的数组?
尝试使用答案。
let ic1 = ref None
...
ic1 := ic;
Error: This expression has type Lwt_io.input Lwt_io.channel
but an expression was expected of type 'a option
let arr = Array.make_matrix 2 2 None
...
arr.(0).(0) <- ic;
Error: This expression has type Lwt_io.input Lwt_io.channel
but an expression was expected of type 'a option
如果它是一个通道数组,那么当您访问某个元素时,您必须获得一个通道。访问此类未初始化数组的元素时,您将获得什么渠道? – newacct
我已经写了一个正确的方式来使用选项类型。你的代码是不一样的。 – camlspotter