Josh
Josh

Reputation: 618

F# new type that is extension of old type

So say I have an employee type

type employee = {
    employee_id: int
    name: string 
    department: int
}

type department = {
   department_id: int
   department_name: string
}

And I want a second type that includes everything in the employee type, but also everything from the department type (in practice the result of an SQL join).

E.g

type employee_extended = {
    employee_id: int
    name: string 
    department: int
    department_id: int
    department_name: string
}

In reality the tables I have have many more columns so just wondering if there was a shorthand to define the extended type :) Can assume no duplicate of property names, but if it's possible to handle them might be useful in the future.

Upvotes: 0

Views: 136

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243126

If you have some data in two types (employee and department) and want to create a new type that contains information about both the employee and the department, the best way is to define a new record type that contains the two other types as fields:

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
}

Upvotes: 3

Related Questions