模块依赖循环

Module dependency cycle

我有:

模块 1

模块 2:

模块 3:

问题

结果我显然得到 dependency cycle: src/Module3.cmj -> src/Module2.cmj -> src/Module3.cmj 编译器错误。在 TypeScript/JS 中通过单独导入可以轻松实现的事情,在 Reason 中是不可能的。如何解决这个问题?

我并不是真的想改变我的程序的架构,只是为了弥补compiler/module系统的缺点。

看起来 Module1 不依赖于其他两个模块。您可以保持原样。但由于其他两个是相互递归的,您可以使用递归模块语法来表达它。这确实有一个要求,尽管您在定义时声明了模块签名,因为 Reason 需要知道会发生什么。例如:

/* Impl.re */
module rec Module2: {
  type type2;
  let make: (Module1.type1, Module3.type3) => type2;
} = {
  ... actual implementation of Module2 ...
} and Module3: {
  type type3;
  let make: Module1.type1 => type3;
  let foo: Module1.type1;
  let bar: Module2.type2 => type3;
} = {
  ... actual implementation of Module3 ...
};

这是您会使用的一般形状,您可以根据需要进行调整。

如果您不希望您的用户必须执行 Impl.Module2.... 才能访问递归模块,您甚至可以使用 include:

将它们公开为文件模块
/* Module2.re */
include Impl.Module2;

您甚至可以使用 compile-time 警告注释实现模块(Impl.Module2 和 3),让用户知道不要使用这些模块:

/* Impl.re */
[@deprecated "Please use Module2 instead of Impl.Module2"]
module Module2: {...} = {...};

处理您的问题的最简单方法确实是递归模块。我不建议您使用它们,因为递归模块会使您的代码更难阅读、编译,并且在最复杂的情​​况下可能会在 运行 时破坏您的代码。更不用说如果您在模块定​​义中使用 side-effects(请不要)。

我将使用 OCaml 语法,您应该能够轻松地翻译成 Reason。

如果您无论如何都想这样做,这里是使用递归模块和函子的快速而肮脏的解决方案。

快速而肮脏的解决方案

1) 创建一个模块 myModTypes,它将指示模块 2 和模块 3 的预期类型。它应该看起来像:

module type Module2type = sig ... end
module type Module3type = sig ... end

其中 ... 是您模块的预期签名(如果您已经编写了接口文件,只需 copy/paste 在这里,如果您不编写这些文件,它们很重要)

2) 将 module2 和 module3 放在期望另一个模块的仿函数中

例如,module2 的代码现在应该是这样的

module MakeModule2(Module3 : MyModTypes.Module3type) = struct
(* the code of module2 *)
end

module3的代码也是一样的,只是在添加的行中交换2和3。

3) 使用该代码创建模块 makemodules2and3(翻译为 Reason):

module rec Module2 : MyModTypes.Module2type = Module2.MakeModule2(Module3)
and Module3 : MyModTypes.Module3type = Module3.MakeModule3(Module2)

请注意,递归模块定义总是需要一个模块类型。

4) Module2Module3 的后续使用现在应该 open Makemodules2and3 才能使用它们。

正确的解决方案

您必须更改程序的体系结构。略.

正如 OP 所说,函数中没有依赖循环,这是一种解脱。只需将 module2 和 module3 分别拆分为两个新模块即可。一个具有仅依赖于 module1 和它们自己的模块的功能,一个具有 "next step" 功能。

这是处理如何声明模块的更好方法:它们应该与它们定义的类型相同。理想情况下,每种类型都有一个模块,再加上类型之间的每次交互都有一个额外的模块。