po prostu zdefiniować moduł Matrix następująco:polimorficzne typu wewnątrz modułu (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
I let m = Matrix.make 3 4 0
daje mi błąd Error: This expression has type int but an expression was expected of type Matrix.element
. Potem dodaje '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
Kompilacja modułu daje błąd Error: Unbound type parameter 'a
.
Czy ktoś mógłby mi powiedzieć, jak zdefiniować typ w moim module?
Mam pytanie, czy jest to możliwe do zrobienia: 'moduł Element = struct typu 'T =' a niech porównać = porównać koniec ;; moduł ElementMap = Map.Make (Element) '? Otrzymuję komunikat "Błąd: niedopasowanie podpisu:" – codablank1
Nie jest to możliwe, ponieważ Map.Make oczekuje typu t bez parametrów. Aby to było możliwe, potrzebna byłaby inna wersja mapy. –