如果我有数据类型标识和人员,我该如何使用它们?
type identification = Name of string
| SS of int * int;;
type person = Personal_info of identification;;
如何创建具有身份识别的人物类型?如何构造一个包含这些类型的变量..?
像这样:
person1 = Personal_info (Name = "Cody") (SS = (231,4534));;
val person1 : person = .....
最佳答案
您的身份
类型指定姓名或社会安全号码。在我看来你想同时拥有它们。为此,您可能应该使用记录类型:
type ident = { name: string; ss: int * int }
由于您的 person
类型只有一种变体,因此它是多余的(并不是说它在某些情况下没有用)。
这是一个 ident
类型的值:
{ name = "Cody"; ss = (231, 4534) }
如果您确实想使用 person
类型,它看起来像这样:
type person = PI of ident
这种类型的值看起来像这样:
PI { name = "Cody"; ss = (231, 4534) }
关于types - OCaml - 创建自定义类型的自定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31779865/