我仍在试图弄清楚在使用 mirage 时如何拆分代码,它有无数的一流模块。我已经把我需要的一切都放在了一个又大又丑的Context
模块中,以避免将十个模块传递给我的所有功能,一个就够痛苦了。
我有一个通过 tcp 接收命令的功能:
let recvCmds (type a) (module Ctx : Context with type chan = a) nodeid chan = ...
经过数小时的反复试验,我发现我需要添加(type a)
“显式”type chan = a
才能使其工作。看起来很难看,但它可以编译。但是,如果我想让该函数递归:
let rec recvCmds (type a) (module Ctx : Context with type chan = a) nodeid chan =
Ctx.readMsg chan >>= fun res ->
... more stuff ...
|> OtherModule.getStorageForId (module Ctx)
... more stuff ...
recvCmds (module Ctx) nodeid chan
我通过了两次模块,第一次没问题,但在递归行上出现错误:
The signature for this packaged module couldn't be inferred.
如果我尝试指定我得到的签名
This expression has type a but an expression was expected of type 'a
The type constructor a would escape its scope
似乎我不能使用整个(type chan = a)
东西。如果有人可以解释正在发生的事情,以及理想的解决方法,那就太好了。我当然可以使用一段时间,但我宁愿最终理解这些该死的模块。谢谢 !