[英]If the signature of a constructor includes the definition of a generic type, what extra responsibilities does that put on users of that constructor?
[英]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)
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.