问题
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 element
s.
回答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