ᆼᆺᆼ*_*ᆼᆺᆼ 1 ocaml module private
是否有可能有一个let绑定(无论是函数、值等)是其模块私有的,并且从外部不可见?
假设我们有A.ml:
let exported = 1
let local = 2
Run Code Online (Sandbox Code Playgroud)
我只想exported从其他模块访问。B.ml:
let a = A.exported
let error = A.local (* This should error *)
Run Code Online (Sandbox Code Playgroud)
类似于let%private在不理性。
这就是签名和 mli 文件背后的动机:它们允许向外部世界隐藏信息,并且只公开 API 的相关部分而不是实现细节。在你的情况下,它看起来像
(* A.ml *)
let exported = 1
let local = 2
Run Code Online (Sandbox Code Playgroud)
和
(* A.mli *)
val exported: int
Run Code Online (Sandbox Code Playgroud)
然后只会exported在A.ml.