You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/fsharp/language-reference/records.md
+21-13Lines changed: 21 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,15 +11,15 @@ Records represent simple aggregates of named values, optionally with members. S
11
11
12
12
```fsharp
13
13
[ 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 ]
20
19
```
21
20
22
21
## Remarks
22
+
23
23
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.
24
24
25
25
Following are some examples.
@@ -43,6 +43,7 @@ The labels of the most recently declared type take precedence over those of the
43
43
Methods can be defined for record types just as for class types.
44
44
45
45
## Creating Records by Using Record Expressions
46
+
46
47
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.
47
48
48
49
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
72
73
```fsharp
73
74
// Rather than use [<DefaultValue>], define a default record.
74
75
type MyRecord =
75
-
{
76
-
field1 : int
77
-
field2 : int
78
-
}
76
+
{ Field1 : int
77
+
Field2 : int }
79
78
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 }
82
81
83
82
// Use the with keyword to populate only a few chosen fields
84
83
// and leave the rest with default values.
85
-
let rr3 = { defaultRecord1 with field2 = 42 }
84
+
let rr3 = { defaultRecord1 with Field2 = 42 }
86
85
```
87
86
88
87
## Pattern Matching with Records
88
+
89
89
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.
@@ -99,17 +99,25 @@ Point is at (10.000000, 0.000000, -1.000000).
99
99
```
100
100
101
101
## Differences Between Records and Classes
102
+
102
103
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.
103
104
104
105
Like union and structure types, records have structural equality semantics. Classes have reference equality semantics. The following code example demonstrates this.
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).
109
116
110
117
If you need reference equality for records, add the attribute `[<ReferenceEquality>]` above the record.
0 commit comments