私はOCamlを初めて使い、Functorについて学んでいます。Functors - インポートタイプ
utop # module type Foo = sig
type t = {
foo : int;
bar : int;
}
val create : int -> int -> t
val get : t -> int
end;;
utop # module FooImpl = struct
type t = {
foo : int;
bar : int;
}
let create x y = {
foo = x;
bar = y;
}
let get w = w.foo
end;;
は、今私はFoo型のモジュールで動作し、get
機能を置き換えます私のファンクタを定義してみましょう:これまでのところ、私は次のよう持っています。
utop # module Functor (F : Foo) : Foo with type t := F.t = struct
let create = F.create
let get w = w.bar
end;;
Error: Unbound record field bar
レコードのタイプについてはわかりません。私はそれを定義してみましょう:
utop # module Functor (F : Foo) : Foo with type t := F.t = struct
type t = {
foo : int;
bar : int;
}
let create = F.create
let get w = w.bar
end;;
Error: Signature mismatch: ... Values do not match:
val get : t -> int
is not included in
val get : F.t -> int
だから、OCamlではt
とF.t
は、実際には同じタイプであることを知りません。だから私はそれを言ってみよう:
utop # module Functor (F : Foo) : Foo with type t := F.t = struct
type t = F.t
let create = F.create
let get w = w.bar
end;;
Error: Unbound record field bar
私は間違って何をしていますか?