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