-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
The 2 recommended multi-line record formattings can be problematic. And in fact trying to use the above recommendations as a beginner was a very frustrating experience. Here's why.
The first multi-line form has editor-based issues that may require manual, line-by-line correction on indent/outdent. I stopped using this form because of the tedious formatting corrections. For example, In VS 2017 outdent will cause later fields to shift out of line with the first field, creating a compiler error until corrected.
// before outdent
let myRecord =
{ Field1 = 1
Field2 = " " }
// after outdent (Shift-Tab), compile error: No assignment given for field Field2
let myRecord =
{ Field1 = 1
Field2 = " " }
The second multi-line form simply does not compile with some type definition features or provides warnings with some constructions.
type IMyInterface = interface end
// compiler error: Unexpected keyword interface
type MyRecord = {
MyField : int
}
interface IMyInterface
// warning: "possible incorrect indentation"
let foo a =
a
|> Option.map (fun x -> {
MyField = x
})
The only record syntax I've found that always "just works" is this.
// this works with all type features, like interface
type MyRecord =
{
SomeField : int
}
interface IMyInterface
// no warning
let foo a =
a
|> Option.map (fun x ->
{
MyField = x
})
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 8c09996f-24ac-cf39-ef43-d7909cbc2b89
- Version Independent ID: 1efdd523-8552-6421-e149-74f76826ef8f
- Content: F# code formatting guidelines
- Content Source: docs/fsharp/style-guide/formatting.md
- Service: unspecified
- Product: dotnet-fsharp
- GitHub Login: @cartermp
- Microsoft Alias: phcart