是否可以在 mli 中为模块添加别名而不创建“必须实现的新模块”。\n这个示例非常人为,但是,例如,假设我有以下源文件\xc2\xa0int_wrapper.ml。
\n\ntype t = Int64.t\n\nlet zero = Int64.of_string "0"\n
Run Code Online (Sandbox Code Playgroud)\n\n我想为它定义一个接口文件,但是\xc2\xa0Int64.t\xc2\xa0很长,所以我想缩写它。
\n\nmodule I = Int64\n\nval zero : int -> I.t\n
Run Code Online (Sandbox Code Playgroud)\n\n当尝试编译模块时,我(可以预见)收到以下错误
\n\nocamlbuild int_wrapper.cmo\n+ ~/.opam/4.03.0/bin/ocamlc.opt -c -o int_wrapper.cmo int_wrapper.ml\nFile "int_wrapper.ml", line 1:\nError: The implementation int_wrapper.ml\n does not match the interface int_wrapper.cmi:\n The module `I\' is required but not provided\nCommand exited with code 2.\nCompilation unsuccessful after building 4 targets (0 cached) in 00:00:00.\nExit 10\n
Run Code Online (Sandbox Code Playgroud)\n\n那是因为 \xc2\xa0 module I = Int64
\xc2\xa0 不是别名。我实际上正在定义一个新模块,它恰好与该模块相同,Int64
并且由于该模块位于签名中,因此我需要在源文件中提供一个实现。\n有没有办法在接口文件中获取真正的别名?
从 OCaml 4.08 开始(请参阅发行说明),现在可以通过本地替换声明实现这一点:
module I := Int64
Run Code Online (Sandbox Code Playgroud)