Skip to content

Commit 2126074

Browse files
shethaaditAdit Sheth
andauthored
Add Example for Mutually Recursive Discriminated Unions in F# (#44610)
* Fixed bug 31809. * Fixed comments. --------- Co-authored-by: Adit Sheth <[email protected]>
1 parent 4901df4 commit 2126074

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

docs/fsharp/language-reference/discriminated-unions.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ Discriminated unions work well if the nodes in the tree are heterogeneous. In th
159159

160160
When this code is executed, the value of `result` is 5.
161161

162+
## Mutually Recursive Discriminated Unions
163+
164+
Discriminated unions in F# can be mutually recursive, meaning that multiple union types can reference each other in a recursive manner. This is useful when modeling hierarchical or interconnected structures. To define mutually recursive discriminated unions, use the `and` keyword.
165+
166+
For example, consider an abstract syntax tree (AST) representation where expressions can include statements, and statements can contain expressions:
167+
168+
```fsharp
169+
type Expression =
170+
| Literal of int
171+
| Variable of string
172+
| Operation of string * Expression * Expression
173+
and Statement =
174+
| Assign of string * Expression
175+
| Sequence of Statement list
176+
| IfElse of Expression * Statement * Statement
177+
```
178+
162179
## Members
163180

164181
It is possible to define members on discriminated unions. The following example shows how to define a property and implement an interface:

0 commit comments

Comments
 (0)