2

我有:

模块 1

  • 提供 type Module1.type1,它的构造函数,以及一些接受和返回的函数type1

模块 2

  • open Module1
  • open Module3
  • 提供类型Module2.type2,也有接受type1type3作为参数的函数

模块 3

  • open Module1
  • open Module2
  • 提供 type Module3.type3,它的构造函数依赖于type1
  • 提供接受和返回类型的函数type1type2以及type3

问题

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

我真的不想改变我的程序的架构,只是为了方便编译器/模块系统的缺点。

4

2 回答 2

5

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

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

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

快速而肮脏的解决方案

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

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

作为您的...模块的预期签名(如果您已经编写了接口文件,只需在此处复制/粘贴它们,如果您不编写这些,它们很重要)

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

例如,module2 的代码现在应该如下所示

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

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

3)使用该代码创建一个模块 makemodules2and3 (翻译为原因):

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

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

4) 以后的使用Module2Module3现在应该open Makemodules2and3在能够使用它们之前。

正确的解决方案

您必须更改程序的体系结构。轻微地。

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

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

于 2018-02-26T18:15:18.947 回答
4

看起来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;

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

/* Impl.re */
[@deprecated "Please use Module2 instead of Impl.Module2"]
module Module2: {...} = {...};
于 2018-02-24T22:51:26.373 回答