Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion docs/fsharp/language-reference/records.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -122,4 +145,4 @@ If you need reference equality for records, add the attribute `[<ReferenceEquali
- [Classes](classes.md)
- [F# Language Reference](index.md)
- [Reference-Equality](https://msdn.microsoft.com/visualfsharpdocs/conceptual/core.referenceequalityattribute-class-%5bfsharp%5d)
- [Pattern Matching](pattern-matching.md)
- [Pattern Matching](pattern-matching.md)