【发布时间】:2023-01-28 18:11:18
【问题描述】:
它们是局部抽象类型。见https://ocaml.org/manual/locallyabstract.html。它们适用于:
- 创建本地模块
let sort_and_deduplicate (type a) compare l =
let module S = Set.Make(struct type t = a let compare = compare end) in
S.elements (S.of_list l)
- 在存在 GADT 的情况下改进模式匹配类型
type _ monoid = Int: int monoid | Float: float monoid
let zero (type a) (m:a monoid): a = match m with
| Int -> 0
| Float -> 0.
- 调试多态函数
let wrong_id (type a) (x:a) = x + 1
Error: This expression has type a but an expression was expected of type int
【讨论】: