F# 新类型是旧类型的扩展

F# new type that is extension of old type

假设我有一个员工类型

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
}

实际上我的表有更多的列所以只是想知道是否有 shorthand 来定义扩展类型:) 可以假定没有重复的 属性 个名称,但如果可以处理它们可能在将来有用。

如果您有一些数据有两种类型(员工和部门),并且想要创建一个包含员工和部门信息的新类型,最好的方法是定义一个包含这两种类型的新记录类型其他类型作为字段:

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
}