为什么无法匹配私有元组?

Val*_*nko 3 ocaml types encapsulation tuples module

Ocaml为类型提供了三种封装:

  • 抽象 - 当我们无法对模块外部的抽象类型对象执行任何操作时(无法读取、创建、更新)
  • public - 当我们可以在模块外部使用公共类型的对象执行所有操作时(可以读取、创建、更新)
  • private - 当我们只能从模块外部的公共类型的对象中读取时(可以读取但不能创建或更新)

一切都按描述进行,但从私有元组中读取任何内容是不可能的。

例如,以下带有记录类型的示例可以正常工作:

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

Run Code Online (Sandbox Code Playgroud)

但不可能用元组提取相关示例中的任何值。

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
Run Code Online (Sandbox Code Playgroud)

编译器给我最后一行代码的错误:

错误:此表达式的类型为 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
Run Code Online (Sandbox Code Playgroud)

错误:此表达式的类型为 Private.t 这不是函数;它不能被应用。

gle*_*nsl 5

长话短说:这会起作用:

let (a, b) = (x :> int * int)
Run Code Online (Sandbox Code Playgroud)

私有记录和变体类型(名义类型)以及私有类型缩写之间存在差异。还有私有行类型,但我们不去那里。

私有记录和变体类型可以被解构,而私有类型缩写则不能。然而,它们可以被强制为其基础类型,这就是上面所做的。

如果您考虑单个 private 的情况,您也许可以直观地理解为什么会这样int。这样做的目的通常是确保您不能随意将其与普通的混合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 *)
let x: int = (my_id :> int) + 3 (* This works, and is unambiguous *)
Run Code Online (Sandbox Code Playgroud)

由于一对ints 就是这样,所以它的行为相同是有道理的(至少对我来说)。

另请参阅OCaml 手册中有关私有类型的部分