Anonymous records
Introduction
Since Fβ― 4.6 (March 2019)
Syntax: same as Record with "fat" braces
{| fields |}{| Age: int |}β signature{| Age = 15 |}β instance
Inline typing: no need to pre-define a named
typeAlternative to Tuples
Allowed in function input/output
β Anonymous type Cβ―
Benefits β
Reduce boilerplate
Improve interop with external systems (JavaScript, SQL...)
Compatible with Cβ― anonymous types: β When a Cβ― API requires an anonymous type, we can give it an anonymous record instance.
Examples (more on this later) :
LINQ projection
Customization of an existing record
JSON serialization
Inline signature
Alias by module
LINQ Projection
π‘ Select a subset of properties
In Cβ―, we would use an anonymous type:
π F# vs C#: Anonymous Records by Krzysztof Kraszewski
Customize an existing record
π‘ An anonymous record can be instantiated from a record instance
JSON serialization
π‘ An anonymous record can be used as an intermediary type to serialize a union in JSON.
Example:
Resulting JSON:
β Format verbose and not practical.
π‘ Solution: Define an anonymous record as "DTO" to serialize a customer.
Resulting JSON:
Signature inline
π‘ Use an anonymous record declared inside a bigger type to reduce cognitive load:
Limits π
Last updated
Was this helpful?