OCaml: List that could contain two types?

∥☆過路亽.° 提交于 2020-02-04 05:24:05

问题


Instead of specifying a int list or string list, could I specify a list whose members must be either strings or ints, but nothing else?


回答1:


You could do:

type element = IntElement of int | StringElement of string;;

and then use a list of elements.




回答2:


One option is polymorphic variants. You can define the type of the list using:

# type mylist = [`I of int | `S of string] list ;;
type mylist = [ `I of int | `S of string ] list

Then define values such as:

# let r : mylist = [`I 10; `S "hello"; `I 0; `S "world"] ;;
val r : mylist = [`I 10; `S "hello"; `I 0; `S "world"]

You have to be careful to add type annotations, though, because polymorphic variants are "open" types. E.g., the following is legal:

# let s = [`I 0; `S "foo"; `B true]
val s : [> `B of bool | `I of int | `S of string ] list =
  [`I 0; `S "foo"; `B true]

To prevent the type of a list from allowing non-integer-or-string values, use an annotation:

# let s : mylist = [`I 0; `S "foo"; `B true];;
This expression has type [> `B of bool ] but is here used with type
  [ `I of int | `S of string ]
The second variant type does not allow tag(s) `B


来源:https://stackoverflow.com/questions/1428743/ocaml-list-that-could-contain-two-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!