Ocaml 为类型提供了三种封装:
一切都按照描述进行,但从私有元组中读取任何内容是不可能的。
例如,以下带有记录类型的示例效果很好:
module Private : sig
type t = private { a : int }
val make : int -> t
end = struct
type t = { a : int }
let make a = { a }
end
let x = Private.make 5
let a = x.a
let Private.{ a = value } = x
但是不可能用元组提取相关示例中的任何值。
module Private : sig
type t = private int * int
val make : int -> int -> t
end = struct
type t = int * int
let make a b = a, b
end
let x = Private.make 5 6
let Private.(a, b) = x
编译器给我最后一行代码的错误:
错误:此表达式的类型为 Private.t,但表达式为 预期类型为 'a * 'b
我尝试了许多不同的读取私有元组的失败尝试。而我的结果是没有办法做到这一点。
可以这样做吗?如果不是,它是否会破坏私有类型的概念,或者我是否错过了我的理解中的某些内容?
注意
我已经检查过,也不可能执行私有函数。
module Private : sig
type t = private int -> int
val make : int -> int -> t
end = struct
type t = int -> int
let make a b = fun x -> if x = b then b else 0
end
let f = Private.make 5 6
let res = f 5
错误:此表达式的类型为 Private.t 这不是函数;它 无法申请。
tl;博士:这会起作用:
let (a, b) = (x :> int * int)
私有记录和变体类型(名义类型)以及私有类型缩写之间存在差异。还有私有行类型,但我们不去那里。
私有记录和变体类型可以被解构,私有类型缩写则不能。然而,它们可以被强制为其基础类型,这就是上面所做的。
如果您考虑单个私有
int
的情况,您也许可以直观地理解为什么会这样。这样做的目的通常是确保您不能使用普通的 mix
自由地 int
它。例如:
module Id : sig
type t = private int
val make : int -> t
end = struct
type t = int
let make a = a
end
let my_id = Id.make 42
let other_id = my_id + 3 (* This is probably not what you want *)
而且由于一对
int
就是这样,所以它的行为是相同的(至少对我来说)是有道理的。