f# - F# 新类型是旧类型的扩展
问题描述
所以说我有一个员工类型
type employee = {
employee_id: int
name: string
department: int
}
type department = {
department_id: int
department_name: string
}
我想要第二种类型,它既包括员工类型中的所有内容,也包括部门类型中的所有内容(实际上是 SQL 连接的结果)。
例如
type employee_extended = {
employee_id: int
name: string
department: int
department_id: int
department_name: string
}
实际上,我有更多列的表,所以只是想知道是否有定义扩展类型的简写:) 可以假设属性名称没有重复,但如果可以处理它们,将来可能会有用。
解决方案
如果您有两种类型(员工和部门)的一些数据,并且想要创建一个包含有关员工和部门的信息的新类型,最好的方法是定义一个包含其他两种类型作为字段的新记录类型:
type Employee = {
EmployeeId: int
Name: string
Department: int
}
type Department = {
DepartmentId: int
Department_name: string
}
// New type containing information about
// an employee and also their department
type EmployeeWithDepartment = {
Employee : Employee
Department : Department
}
推荐阅读
- .htaccess - 生产中的内部服务器错误,但不是在本地,而不是在“/”路由上
- c - C - while 循环不会退出
- oracle - Oracle 过程中的插入语句无法正常工作
- angular - 异步加载子节点的 Mat-tree 将子节点添加到级别的最后一个节点
- javascript - 如何使用 JQuery UI 自动完成阻止事件在 IE11 中传播?
- github - 告诉 github 使用自定义文件显示为项目的 README
- azure - 使用列标记在 Azure 数据目录中搜索数据集
- regex - vscode 正则表达式搜索?
- python - 是否可以使用 exec() 运行缩进块?
- react-native - react-native init AwesomeProject - 无法构建