GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing learn.microsoft.com — Records in F# Note Access to this page requires authorization. You can try signing in or changing directories. Access to this page requires authorization. You can try changing directories. Records (F#)Records represent simple aggregates of named values, optionally with members. They can either be structs or reference types. They are reference types by default. Syntax
The RemarksIn 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 Following are some examples.
When each label is on a separate line, the semicolon is optional. You can set values in expressions known as record expressions. The compiler infers the type from the labels used (if the labels are sufficiently distinct from those of other record types). Braces ({ }) enclose the record expression. The following code shows a record expression that initializes a record with three float elements with labels
Do not use the shortened form if there could be another type that also has the same labels.
The labels of the most recently declared type take precedence over those of the previously declared type, so in the preceding example,
Methods can be defined for record types just as for class types. Creating Records by Using Record ExpressionsYou 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. The following example shows how to create a record.
The semicolons after the last field in the record expression and in the type definition are optional, regardless of whether the fields are all in one line. When you create a record, you must supply values for each field. You cannot refer to the values of other fields in the initialization expression for any field. In the following code, the type of
Another form of record construction can be useful when you have to copy an existing record, and possibly change some of the field values. The following line of code illustrates this.
This form of the record expression is called the copy and update record expression. Records are immutable by default; however, you can easily create modified records by using a copy and update expression. You can also explicitly specify a mutable field.
Don't use the DefaultValue attribute with record fields. A better approach is to define default instances of records with fields that are initialized to default values and then use a copy and update record expression to set any fields that differ from the default values.
Compose records with spreadsTo reuse fields across different models, F# 11 provides type spreads ( Record constructorsTo identify a record type directly at its construction site, use its all-fields constructor: Note Record constructors are an F# 11.2 feature. In the .NET 11.0.100 SDK, enable
Positional arguments follow field order; named arguments avoid that dependency. Representation accessibility still applies, and a same-named value takes precedence. Constructor patterns aren't supported. Creating Mutually Recursive RecordsSometime when creating a record, you may want to have it depend on another type that you would like to define afterwards. This is a compile error unless you define the record types to be mutually recursive. Defining mutually recursive records is done with the For example, the following code defines a
To create instances of both, you do the following:
If you were to define the previous example without the Pattern Matching with RecordsRecords 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.
The output of this code is as follows.
Records and membersYou can specify members on records much like you can with classes. There is no support for fields. A common approach is to define a
If you use a self identifier, that identifier refers to the instance of the record whose member is called:
Accessibility Modifiers on RecordsThe following code illustrates the use of accessibility modifiers. There are three files in the project:
In the
In the
For more information about accessibility modifiers, see the Access Control article. Differences Between Records and ClassesRecord fields differ from class fields 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. Like union and structure types, records have structural equality semantics. Classes have reference equality semantics. The following code example demonstrates this.
The output of this code is as follows:
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 If you need reference equality for records, add the attribute See also
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
FeedbackWas this page helpful? NoAdditional resources
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |