【发布时间】:2020-08-22 17:40:56
【问题描述】:
我显然是 F# 的新手。来自 C#,我在区分联合方面遇到了困难。
假设 F# 中有以下定义:
type Details =
| ContactDetails of ContactDetail * id: Guid
| Internet of Internet * id: Guid
| PhoneNumbers of PhoneNumber * id: Guid
| Addresses of Address * id: Guid
let contactDetail : ContactDetail = {Name="Contact Detail"; Content="Content for Contact Detail"; Text="here is the contact detail text" }
let internet : Internet = {Name="Internet"; Content="Content for Internet"; Text="here is the internet text" }
let phoneNumber : PhoneNumber = {Name="Phone Number"; Content="Content for phone number"; Text="here is the phone number text" }
let address : Address = {Name="Address"; Content="Content for Address"; Text="here is the Address text" }
let details = [ContactDetails (contactDetail,Guid.NewGuid())
Internet (internet,Guid.NewGuid())
PhoneNumbers (phoneNumber,Guid.NewGuid())
Addresses (address,Guid.NewGuid())
]
type Model = {
Details: Details list
}
如何编写接收模型并返回列表中每个项目的id的函数?
例如:
有趣的细节 -> detail.id
“详细信息”类型未定义字段、构造函数或成员“id”
TIA
编辑#1:
新模型是:
类型模型 = { 详细信息:DetailsWithId 列表 }
【问题讨论】:
标签: f#