Tôi chỉ xác định một mô-đun Matrix như sau:đa hình loại bên trong một mô-đun (OCaml)
module Matrix =
struct
type element
type t = element array array
let make (nr: int) (nc: int) (init: element) : t =
let result = Array.make nr (Array.make nc init) in
for i = 0 to nr - 1 do
result.(i) <- Array.make nc init
done;
result
end
Và let m = Matrix.make 3 4 0
mang lại cho tôi một lỗi Error: This expression has type int but an expression was expected of type Matrix.element
. Sau đó, tôi đã thêm 'a
:
module Matrix =
struct
type element = 'a
type t = element array array
let make (nr: int) (nc: int) (init: element) : t =
let result = Array.make nr (Array.make nc init) in
for i = 0 to nr - 1 do
result.(i) <- Array.make nc init
done;
result
end
Việc biên soạn mô-đun đưa ra lỗi Error: Unbound type parameter 'a
.
Có ai cho tôi biết cách xác định loại bên trong mô-đun của mình không?
Tôi có câu hỏi, có thể thực hiện: 'module Element = struct nhập 'a t =' a để so sánh = so sánh kết thúc ;; module ElementMap = Map.Make (Element) '? Tôi nhận được "Lỗi: Chữ ký không khớp:" – codablank1
Điều đó là không thể, bởi vì Map.Make mong đợi một kiểu tham số ít hơn t. Bạn sẽ cần một phiên bản khác của Bản đồ để làm cho điều đó có thể. –