diff --git a/docs/fsharp/language-reference/records.md b/docs/fsharp/language-reference/records.md index edf9490c2ae31..4c17acfe23c16 100644 --- a/docs/fsharp/language-reference/records.md +++ b/docs/fsharp/language-reference/records.md @@ -84,6 +84,29 @@ let defaultRecord2 = { Field1 = 1; Field2 = 25 } let rr3 = { defaultRecord1 with Field2 = 42 } ``` +## Creating Mutually Recursive Records + +Sometime when creating a record, you may want to have it depend on another type that you would like to define afterwards. This is a compile error unless you define the record types to be mutually recursive. + +Defining mutually recursive records is done with the `and` keyword. This lets you link 2 or more record types together. + +For example, the following code defines a `Person` and `Address` type as mutually recursive: + +```fsharp +// Create a Person type and use the Address type that is not defined +type Person = + { Name: string + Age: int + Address: Address } +// Define the Address type which is used in the Person record +and Address = + { Line1: string + Line2: string + PostCode: string } +``` + +If you were to define the previous example without the `and` keyword, then it would not compile. The `and` keyword is required for mutually recursive definitions. + ## Pattern Matching with Records Records can be used with pattern matching. You can specify some fields explicitly and provide variables for other fields that will be assigned when a match occurs. The following code example illustrates this. @@ -122,4 +145,4 @@ If you need reference equality for records, add the attribute `[