Home

Mutually Recursive Records

   

Description

Mutually recursive records are types that follow each other in the document they are created. The first record is created with the type keyword. The other records that follow the first are created using the and keyword. The formula to follow is:

type record-name-1 = {
    labels }
and record-name-2 = {
    labels }
and record-name--n = {
    labels }

The records don't have to share anything. They just have to be created one after the other. Still, the biggest feature about mutually recursive records is that the first record can refer to a record created after it.  For example, the following code will produce an error:

type Employee = {
    EmployeeNumber : string
    FirstName      : string
    LastName       : string
    Department     : Department
    Title          : string }

type Department = {
    DepartmentCode : string
    DepartmentName : string }

let dept = {
    DepartmentCode = "ADMN";
    DepartmentName = "Administration, Admissions, and Students Affairs" }
let staff = {
    EmployeeNumber = "161138"
    FirstName = "Laura"
    LastName = "Fannie"
    Department = { DepartmentCode = "ADMN"; DepartmentName = "Administration, Admissions, and Students Affairs" }
    Title = "Dean of Litterary Studies" }

The error is because the compiler works from top-bottom but when it encounters the Department type, it cannot reconcile it. The solution is to crecord the second or subsequent record(s) using the and keyword. Here is an example:

type Employee = {
    EmployeeNumber : string
    FirstName      : string
    LastName       : string
    Department     : Department
    Title          : string }

and Department = {
    DepartmentCode : string
    DepartmentName : string }

let dept = {
    DepartmentCode = "ADMN";
    DepartmentName = "Administration, Admissions, and Students Affairs" }
let staff = {
    EmployeeNumber = "161138"
    FirstName = "Laura"
    LastName = "Fannie"
    Department = { DepartmentCode = "ADMN"; DepartmentName = "Administration, Admissions, and Students Affairs" }
    Title = "Dean of Litterary Studies" }
     
     
 

Home Copyright © 2015 FunctionX Home