如何在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