如何引用作为模块参数传入的functor所包含的函数
你收到报错的核心原因是,作为参数传入的MakeCollection是函子(functor),本质是用于生成模块的“高阶模块”,本身不是可直接访问成员的普通模块,自然不能直接调用MakeCollection.add。
你不需要自行实现该函子的逻辑,只需要在MakePuzzleSolver内部,根据你要存储的元素类型实例化这个函子,得到具体的集合模块后就能调用对应的add等方法,示例如下:
module MakePuzzleSolver (MakeCollection : functor (Element : sig type t end) -> (Collections.COLLECTION with type elt = Element.t)) (Puzzle : PUZZLEDESCRIPTION) : (PUZZLESOLVER with type state = Puzzle.state and type move = Puzzle.move) = struct (* 实例化函子:生成存储Puzzle.state类型的集合模块 *) module StateCollection = MakeCollection(struct type t = Puzzle.state end) (* 实例化函子:生成存储Puzzle.move类型的集合模块 *) module MoveCollection = MakeCollection(struct type t = Puzzle.move end) (* 之后就可以正常调用COLLECTION签名定义的所有方法 *) let demo_usage initial_state = let empty_coll = StateCollection.empty in StateCollection.add initial_state empty_coll end
所有Collections.COLLECTION签名中定义的函数,都可以通过你实例化得到的StateCollection/MoveCollection等模块直接调用。
内容的提问来源于stack exchange,提问作者Ash Ketchum
相关产品推荐
相关产品推荐

