OCaml:包括模块但使类型抽象
OCaml: Including module but making type abstract
假设我想扩展 Array
模块,
module type VEC =
sig
include module type of Array
type 'a t
(* other stuff *)
end
具体实施
module Vec : VEC = struct
include Array
type 'a t = 'a array
(* other stuff *)
end
使用 include Array
使我能够继续使用 Array
模块中的函数并且还可以访问运算符。但是,如果我调用 Vec.make 4 0
,它将 return 和 int array
。
我想要它做的是能够继续使用 Array
模块中的功能,但让它们 return int Vec.t
代替。我想知道这是否可能?
除了在 VEC
的定义中使用 'a t
重新声明 Array
的函数外,恐怕没有简单的方法:
module type VEC = sig
type 'a t
(* Instead of include module type of Array, hand write all the types of
the function in Array, replacing 'a array to 'a t *)
val length : 'a t -> int
val get : 'a t -> int -> 'a
...
end
module Vec : VEC = struct
type 'a t = 'a array
include Array
end
假设我想扩展 Array
模块,
module type VEC =
sig
include module type of Array
type 'a t
(* other stuff *)
end
具体实施
module Vec : VEC = struct
include Array
type 'a t = 'a array
(* other stuff *)
end
使用 include Array
使我能够继续使用 Array
模块中的函数并且还可以访问运算符。但是,如果我调用 Vec.make 4 0
,它将 return 和 int array
。
我想要它做的是能够继续使用 Array
模块中的功能,但让它们 return int Vec.t
代替。我想知道这是否可能?
除了在 VEC
的定义中使用 'a t
重新声明 Array
的函数外,恐怕没有简单的方法:
module type VEC = sig
type 'a t
(* Instead of include module type of Array, hand write all the types of
the function in Array, replacing 'a array to 'a t *)
val length : 'a t -> int
val get : 'a t -> int -> 'a
...
end
module Vec : VEC = struct
type 'a t = 'a array
include Array
end