f# - 语法:在 F# 中返回可区分联合的参数的函数?(函数参数的模式匹配)
问题描述
我显然是 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 列表 }
解决方案
可区分联合中的“已区分”意味着每个字段定义都被认为是彼此不同的,这就是为什么即使您有一个公共字段,也不能直接访问它。
我通常做的最简单的事情是创建一个安全地提取该字段的扩展函数。
type Details =
| ContactDetails of ContactDetail * id: Guid
| Internet of Internet * id: Guid
| PhoneNumbers of PhoneNumber * id: Guid
| Addresses of Address * id: Guid
member this.id =
match this with
| ContactDetail(_, id)
| Internet(_, id)
| PhoneNumber(_, id)
| Address(_, id) -> id
另一种选择是以不同的方式构建数据:
type Details =
| ContactDetails of ContactDetail
| Internet of Internet
| PhoneNumbers of PhoneNumber
| Addresses of Address
// Each instance will hold one of ContactDetails, Internet, Phonenumber, and
// each has a common property of ID (the Guid). This way, you simplify your model
type DetailsWithId = DetailsWithId of Details * Guid
另一句话:您对 DU 的每个成员使用复数形式,但定义只允许一个项目。
推荐阅读
- php - PHP + curl:FTP TLS 如何在我的服务器和 FTP 之间匹配 TLS 版本
- javascript - Javascript单选按钮检查条件
- ruby-on-rails - Sidekiq 和 puma 共享同一个数据库连接池吗
- powershell - GUI中的Powershell Write-Progress命令在完成之前停止
- python - RuntimeError:当 `enforce_sorted` 为 True 时,`lengths` 数组必须按降序排序。- 火炬
- reactjs - React Apollo 客户端多个突变
- mongodb - 并发请求的MongoDB内存管理
- webpack - 当extractCss为真时,如何在head标签中获取特定css文件的内联样式?
- javascript - 加载所有图像后更新组件的状态
- swift - RealityKit – 如何为自然光创建阴影捕捉器?