簡體   English   中英

在 OCaml 中,定義或 function 簽名中的(類型 v)中的“括號類型”是什么?

[英]in OCaml, what's "parenthesis type" as in (type v) in a definition or in a function signature?

我知道泛型類型,但有時我在代碼中看到let a (type vw) etc. ,有什么區別?

它們是局部抽象類型。 請參閱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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM