You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何引用作为模块参数传入的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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 03:57:04