此代码给出“未绑定模块类型测试”:
module type Test = sig
val test : int -> (module Test)
end
我该如何进行这项工作?
返回与正在构建的模块相同的模块显然需要递归。但是,递归模块类型不能直接只允许递归模块。因此,解决方案是将模块类型包装在递归模块中:
module rec Test: sig
module type t = sig val test: int -> (module Test.t) end
end = Test
请注意,上面的代码使用经典技巧来避免在没有任何运行时组件的情况下编写两次模块类型。然后,新的模块类型可以用于:
module rec X: Test.t = struct let test x = (module X:Test.t) end;;
module T = (val X.test 1)
module T2 = (val T.test 2)