Variants or Polymorphic variants?

前端 未结 3 658
萌比男神i
萌比男神i 2020-12-23 11:25

I noticed that, among OCaml programmers I know, some of them always use polymorphic variants (variants that are not declared, prefixed with a backquote), while othe

3条回答
  •  别那么骄傲
    2020-12-23 12:16

    The only reason why I use polymorphic variants in most module interfaces is to work around the naming issues of classic variants.

    If the following could work, polymorphic variants would no longer be useful in a majority of cases:

    type t1 = String of string | Int of int | Bool of bool | List of t1 list
    type t2 = String of string | Int of int | Other
    
    let simplify x =
      match (x : t1) with
          String s -> String s
        | Int n -> Int n
        | Bool _
        | List _ -> Other
    

    2014-02-21 Update: the code above is now valid in OCaml 4.01. Hurray!

提交回复
热议问题