Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 16 additions & 1 deletion docs/fsharp/language-reference/lazy-expressions.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Lazy Expressions
description: Learn how F# lazy expressions can improve the performance of your apps and libraries.
ms.date: 08/15/2020
ms.date: 10/02/2025
---
# Lazy Expressions

Expand All @@ -27,6 +27,21 @@ The following code illustrates the use of lazy expressions and the use of `Force

Lazy evaluation, but not the `Lazy` type, is also used for sequences. For more information, see [Sequences](sequences.md).

## Formatting

For multiline lazy expressions, place the opening parenthesis on the same line as the `lazy` keyword, with the expression body indented one level:

```fsharp
let expensiveCalculation =
lazy (
let step1 = performStep1()
let step2 = performStep2 step1
step2 * 2
)
```

For more information on formatting lazy expressions, see the [F# formatting guide](../style-guide/formatting.md#formatting-lazy-expressions).

## See also

- [F# Language Reference](index.md)
Expand Down
37 changes: 36 additions & 1 deletion docs/fsharp/style-guide/formatting.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: F# code formatting guidelines
description: Learn guidelines for formatting F# code.
ms.date: 11/01/2023
ms.date: 10/02/2025
---
# F# code formatting guidelines

Expand Down Expand Up @@ -539,6 +539,41 @@ let useAddEntry () =
bar ()
```

### Formatting lazy expressions

When writing single-line lazy expressions, keep everything on one line:

```fsharp
// ✔️ OK
let x = lazy (computeValue())

// ✔️ OK
let y = lazy (a + b)
```

For multiline lazy expressions, place the opening parenthesis on the same line as the `lazy` keyword, with the expression body indented one level and the closing parenthesis aligned with the opening:

```fsharp
// ✔️ OK
let v =
lazy (
// some code
let x = computeExpensiveValue()
let y = computeAnotherValue()
x + y
)

// ✔️ OK
let handler =
lazy (
let connection = openConnection()
let data = fetchData connection
processData data
)
```

This follows the same pattern as other function applications with multiline arguments. The opening parenthesis stays with `lazy`, and the expression is indented one level.

### Formatting arithmetic and binary expressions

Always use white space around binary arithmetic expressions:
Expand Down
Loading