作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
很抱歉发布如此长的、不可编译的代码。但是,尽管阅读了关于 ocaml 仿函数上 stackoverflow 的几个问题和答案,但我不知道如何解决这个问题:
假设我有一个非常抽象的数据结构:ads.mli
module type ENTRY = sig
type t
val get_index : t -> int
val compare : t -> t -> int
end
module type T = sig
type entry
type t
val create : unit -> t
val insert : entry -> t -> t
val delete : entry -> t -> t
end
基于此,我可以通过传递一个仿函数来对这些抽象实现进行具体的数据结构。例如我做了:
concrete_ads.mli
module Make (Entry: Ads.ENTRY) : (ads.T with type entry = Entry.t)
这项工作,我现在可以在其他源文件中使用我的实现,例如:
module AT = Concrete_ads.Make(
type t = int * int;;
let get_index = fst;;
let to_string = (fun (x,y) -> Printf "%i, %i" x y);;
end);;
然后,使用如下实现:
let at = AT.create () in
let ati = AT.insert (1,2) at in
let atd = AT.delete (1,2) ati in
... ETC。
search.mli
val search : Int -> Ads.T -> int list
但是,编译时我得到:
Failure: "invalid long identifier type"
然后,我认为我需要专门将 adt 的模块声明为
search.mli
中的子模块, 就像是:
search.mli
module AD = Ads;;
...
val search : Int -> AD.T -> int list
但是,我得到:
Parse error: [module_declaration] expected after [a_UIDENT] (in [sig_item])
我在这里想念什么?我觉得我要么在语法上失败了,要么没有完全掌握 Functors、Modules 和 Submodules 的概念......
Ads.T
。 ,但需要
Ads.T.t
的特定类型.
search.mli
:
module Make (T : Ads.T with type entry = int * int) : sig
val search : T.t -> int -> int
end;;
并且,在
search.ml
:
module Make (T : Ads.T with type entry = int * int) : sig
val search : T.t -> int -> int
end = struct
(* actual implementation of search *)
end;;
它完全按照我的意图工作。
最佳答案
你到底想做什么?您是否希望您的函数通过广告类型(例如 Ads.T.t
)或广告模块(例如 Ads.T
)进行参数化?
在这两种情况下,您都应该将这些通用函数包装在模块中:
module Generic (Ad : Ads.T) : sig
val search : int -> Ad.t -> int list
end = struct
let search _ _ = assert false
end
Conrete_ads
合作模块:
module AT = Concrete_ads.make(struct ... end)
module Lib = Generic(AT)
let foo = Lib.search 2 (AT.create ())
val search : int -> AT.t -> int list
AT
的定义中,您忘记了
struct
在
struct .. end
仿函数的模块参数。
val search : int -> (module Ad.T) -> int list
let search n ad_module =
let module Ad = (val ad_module : Ad.T) in
... Ad.foo ..
... search 2 (module AT : Ad.T) ...
module S
作为类型表达式,是签名
S
的“具体模块”的值的类型;
(val t : S)
作为模块表达式,是被打包到值
t
中的模块, 带有签名
S
。这里我将
ad_module
作为一个值并在本地解包到
Ad
模块中,然后可以将其用作函数内部的任何其他模块。最后,
(module M : S)
是一个术语表达式将带有签名
M
的模块
S
打包成一个值。)
关于module - Ocaml 仿函数、模块和子模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907435/
我是一名优秀的程序员,十分优秀!