Skip to content

Commit a90328a

Browse files
authored
Update records as per feedback (#5610)
1 parent 817086f commit a90328a

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

docs/fsharp/language-reference/records.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ Records represent simple aggregates of named values, optionally with members. S
1111

1212
```fsharp
1313
[ attributes ]
14-
type [accessibility-modifier] typename = {
15-
[ mutable ] label1 : type1;
16-
[ mutable ] label2 : type2;
17-
...
18-
}
19-
[ member-list ]
14+
type [accessibility-modifier] typename =
15+
{ [ mutable ] label1 : type1;
16+
[ mutable ] label2 : type2;
17+
... }
18+
[ member-list ]
2019
```
2120

2221
## Remarks
22+
2323
In the previous syntax, *typename* is the name of the record type, *label1* and *label2* are names of values, referred to as *labels*, and *type1* and *type2* are the types of these values. *member-list* is the optional list of members for the type. You can use the `[<Struct>]` attribute to create a struct record rather than a record which is a reference type.
2424

2525
Following are some examples.
@@ -43,6 +43,7 @@ The labels of the most recently declared type take precedence over those of the
4343
Methods can be defined for record types just as for class types.
4444

4545
## Creating Records by Using Record Expressions
46+
4647
You can initialize records by using the labels that are defined in the record. An expression that does this is referred to as a *record expression*. Use braces to enclose the record expression and use the semicolon as a delimiter.
4748

4849
The following example shows how to create a record.
@@ -72,20 +73,19 @@ Don't use the DefaultValue attribute with record fields. A better approach is to
7273
```fsharp
7374
// Rather than use [<DefaultValue>], define a default record.
7475
type MyRecord =
75-
{
76-
field1 : int
77-
field2 : int
78-
}
76+
{ Field1 : int
77+
Field2 : int }
7978
80-
let defaultRecord1 = { field1 = 0; field2 = 0 }
81-
let defaultRecord2 = { field1 = 1; field2 = 25 }
79+
let defaultRecord1 = { Field1 = 0; Field2 = 0 }
80+
let defaultRecord2 = { Field1 = 1; Field2 = 25 }
8281
8382
// Use the with keyword to populate only a few chosen fields
8483
// and leave the rest with default values.
85-
let rr3 = { defaultRecord1 with field2 = 42 }
84+
let rr3 = { defaultRecord1 with Field2 = 42 }
8685
```
8786

8887
## Pattern Matching with Records
88+
8989
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.
9090

9191
[!code-fsharp[Main](../../../samples/snippets/fsharp/lang-ref-1/snippet1910.fs)]
@@ -99,17 +99,25 @@ Point is at (10.000000, 0.000000, -1.000000).
9999
```
100100

101101
## Differences Between Records and Classes
102+
102103
Record fields differ from classes in that they are automatically exposed as properties, and they are used in the creation and copying of records. Record construction also differs from class construction. In a record type, you cannot define a constructor. Instead, the construction syntax described in this topic applies. Classes have no direct relationship between constructor parameters, fields, and properties.
103104

104105
Like union and structure types, records have structural equality semantics. Classes have reference equality semantics. The following code example demonstrates this.
105106

106107
[!code-fsharp[Main](../../../samples/snippets/fsharp/lang-ref-1/snippet1911.fs)]
107108

109+
The output of this code is as follows:
110+
111+
```
112+
The records are equal.
113+
```
114+
108115
If you write the same code with classes, the two class objects would be unequal because the two values would represent two objects on the heap and only the addresses would be compared (unless the class type overrides the `System.Object.Equals` method).
109116

110117
If you need reference equality for records, add the attribute `[<ReferenceEquality>]` above the record.
111118

112119
## See Also
120+
113121
[F# Types](fsharp-types.md)
114122

115123
[Classes](classes.md)

0 commit comments

Comments
 (0)