Anonymous record type inside discriminated union
algebraic-data-types, f#
Solution
You can define mutually dependent types using `and`:
type Employee =
| Engineer of Person
| Manager of Manager // manager has list of reports
| Executive of Executive
and Manager = { Name: Person; Reports: Employee list }
and Executive = { Name: Person; Reports: Employee list; Assistant: Employee }
Problem
The F# tutorial includes the following snippet: ``` /// A record for a person's first and last name type Person = { First : string Last : string } /// define a discriminated union of 3 different kinds of employees type Employee = | Engineer of Person | Manager of Person * list<Employee> // manager has list of reports | Executive of Person * list<Employee> * Employee // executive also has an assistant ``` The fact that Manager and Executive are described as tuples offends my sensibilities (I'm easily offended). It strikes me as not terribly expressive. I tried to modify them as follows: ``` /// define a discriminated union of 3 different kinds of employees type Employee = | Engineer of Person | Manager of { Name: Person; Reports: Employee list } // manager has list of reports | Executive of { Name: Person; Reports: Employee list; Assistant: Employee} // executive also has an assistant ``` Sadly, the definitions of Manager and Executive now give an error: "This construct is deprecated; consider using a separate record type instead." Ok, seems fair, let's call it ManagerType. But wait ... ManagerType refers to Employee (for the Reports part), and Employee refers to ManagerType (for the Manager option). Is there a solution here? Is it not possible for two data structures to be defined in terms of each other?