module - OCaml 在实现中重复整个签名
问题描述
假设我有一个模块A
,具有以下接口和实现文件:
(* a.mli *)
module type A_SIG =
sig
val f: int -> int
...
end
(* a.ml *)
module A : A_SIG =
struct
let f x = x + 1
...
end
编译a.mli
后a.ml
失败,出现错误Unbound module type A_SIG
。在实现文件中复制整个签名可以修复它。
为什么会这样?似乎以下在 SML 中有效:
(* a.sig *)
signature A_SIG =
sig
val f: int -> int
...
end
(* a.sml *)
structure A : A_SIG =
struct
fun f x = x+1
...
end
我看过这个类似的帖子,但我不清楚它的答案。
解决方案
文件a.ml
被隐式包装为模块A
,文件a.mli
被隐式包装为模块类型A
。
所以以下工作
(* A.mli *)
val f : int -> int
(* A.ml *)
let f x = x + 1
您可以f
从另一个模块访问A.f
.
或者如果你真的想要子模块,你可以写
(* a.mli *)
module type A_SIG =
sig
val f: int -> int
end
module A : A_SIG
(* a.ml *)
module type A_SIG =
sig
val f: int -> int
end
module A : A_SIG =
struct
let f x = x + 1
end
并且您可以f
从另一个模块访问,并且除了 (sub)module 之外,A.A.f
该模块A
还将包含签名。A_SIG
A
标准 ML 的实现通常不会将文件的内容隐式包装为模块。
为了完整起见,请注意 OCaml 具有从模块“生成”模块类型的功能:
(* a.mli *)
module type A_SIG =
sig
val f: int -> int
end
module A : A_SIG
(* a.ml *)
module A =
struct
let f x = x + 1
end
module type A_SIG = module type of A
推荐阅读
- php - 我在 php 中得到一个未定义的 curl_version() 函数
- apache-spark - 5 分钟 Spark 批处理作业与流式作业
- python - Matplotlib 表格 Y 轴对齐图表
- excel - 您可以在不选中启用所有宏的情况下运行 VBA excel
- python - 当不存在公共记录时,Pandas 合并删除添加的列
- node.js - 如何在节点js+firebird中执行存储过程
- android - 无法使用谷歌登录
- codenameone - Codenameone:如何在 Android 上访问日历
- javascript - 如何创建一个后端来隐藏 Reactjs 应用程序中的 API 密钥?
- java - 我的程序没有显示代码?