BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News F# 4.6 Released with Anonymous Records, Improved Performance

F# 4.6 Released with Anonymous Records, Improved Performance

This item in japanese

The most significant change in F# 4.6, now available with .NET Core 2.2, is the introduction of anonymous record types, which simplify the use of records in a number of contexts. Additionally, the F# compiler chain in Visual Studio 2019 has improved performance for medium-to-large size projects in several areas.

Anonymous record types allow developers to declare record types ad-hoc without specifying a name upfront for them. This opens up new possibilities such as in the following snippet:

let foo (args: {| a: Int; b: Int |}) =
  a + b

foo {| a=2; b=4 |}

{| a = 1+1 |} = {| a = 2 |} // true

{| a = 1+1 |} = {| a = 2;  b = 1|} // error, record type mismatch

Although syntactically they could suggest to be using structural typing, anonymous record types do not introduce structural typing in F#. As an effect of that, two anonymous recod types will only be recognised as the same type if their fields use the same labels and are of the same type. Likewise, they do not support pattern matching. Anonymous record types can be serialized and deserialized just like normal records and can be stack-allocated using the syntax struct {| ... |}. It is important to observe that anonymous record types and traditional record types are not interchangeable, so if you have a named record type with the same definition of an anonymous record type, you still will not be able to use them interchangeably.

According to Microsoft .NET and languages program manager Philip Carter, most of the time you will keep using named record types in your code, but there are a number of scenarios where anonymous record types help developers get more concise code, such as when defining recursive sum types, using LINQ. More generally, anonymous record types will makes it easier to use structured ephemeral data, which could be a typical use case for data scientist. InfoQ already covered F# anonymous record types when they first became available with F# 4.6 preview. For a more complete description of those cases, see our previous article.

Another area where Microsoft has improved the F# ecosystem is tooling performance. Indeed, the latest release of the F# tools for Visual Studio 2019 focused on performance for medium-to-large size solutions, i.e., solutions containing more than 50 projects. Specifically, the new release has improved both memory and CPU usage. This was made possible by identifying cases where Large Object Heap allocation was being inefficient. Additional work went into reducing cache sizes, improving format string processing, and several optimizations which should improve the F# experience, according to Microsoft. Unfortunately, Microsoft has not released any figures to allow the better understanding of the range of these improvements.

A final mention goes to a couple of new features in the F# core library, including improved ValueOptions, which are now on-a-par with the Option type, and support for tryExactlyOne on List, Array, and Seq, returning an option type instead of throwing an exception if no element is found.

To get F# 4.6, you can install Visual Studio 2019 if you are on Windows, or you can install .NET Core 2.2 and Visual Studio for Mac, or Visual Studio Code with Ionide.

Rate this Article

Adoption
Style

BT