Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FSharp: Add chained expressions guidance. #33524

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Changes from 1 commit
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
96 changes: 96 additions & 0 deletions docs/fsharp/style-guide/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,102 @@ In single-line expressions the delimiter symbols should be placed on the same li
@>
```

### Formatting chained expressions

When chained expressions (function applications intertwined with `.`) are long, put each application invocation on the next line.
Indent the subsequent links in the chain by one level after the leading link.

```fsharp
// ✔️ OK
Host
.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(fun webBuilder -> webBuilder.UseStartup<Startup>())

// ✔️ OK
Cli
.Wrap("git")
.WithArguments(arguments)
.WithWorkingDirectory(__SOURCE_DIRECTORY__)
.ExecuteBufferedAsync()
.Task
```

The leading link can be composed out of multiple links if they are simple identifiers.
For example, the addition of a fully qualified namespace.

```fsharp
// ✔️ OK
Microsoft.Extensions.Hosting.Host
.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(fun webBuilder -> webBuilder.UseStartup<Startup>())
BillWagner marked this conversation as resolved.
Show resolved Hide resolved
```

Subsequent links should also contain simple identifiers.

```fsharp
// ✔️ OK
configuration.MinimumLevel
.Debug()
// Notice how `.WriteTo` does not need its own line.
.WriteTo.Logger(fun loggerConfiguration ->
loggerConfiguration.Enrich
.WithProperty("host", Environment.MachineName)
.Enrich.WithProperty("user", Environment.UserName)
.Enrich.WithProperty("application", context.HostingEnvironment.ApplicationName))
```

When the arguments inside a function application don't fit on the rest of the line, put each argument on the next line.

```fsharp
// ✔️ OK
WebHostBuilder()
.UseKestrel()
.UseUrls("http://*:5000/")
.UseCustomCode(
longArgumentOne,
longArgumentTwo,
longArgumentThree,
longArgumentFour
)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build()

// ✔️ OK
Cache.providedTypes
.GetOrAdd(cacheKey, addCache)
.Value

// ❌ Not OK, formatting tools will reformat to the above
Cache
.providedTypes
.GetOrAdd(
cacheKey,
addCache
)
.Value
```

Lambda arguments inside a function application should start on the same line as the opening `(`.

```fsharp
// ✔️ OK
builder
.WithEnvironment()
.WithLogger(fun loggerConfiguration ->
// ...
())

// ❌ Not OK, formatting tools will reformat to the above
builder
.WithEnvironment()
.WithLogger(
fun loggerConfiguration ->
// ...
()
)
BillWagner marked this conversation as resolved.
Show resolved Hide resolved
```

## Formatting declarations

This section discusses formatting declarations of different kinds.
Expand Down