Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News What's New in F# 8.0: Simpler, Enhanced Uniformity, and Improved Performance

What's New in F# 8.0: Simpler, Enhanced Uniformity, and Improved Performance

Back in November, during the .NET Conf Microsoft released F# 8.0 as part of the .NET 8 release. The new version of F# brings in many features to make F# code simpler, more uniform, and apps more performant. The new features are related to language changes, improved diagnostics, quality-of-life improvements, and performance boosts for project compilation and upgrades.

In the latest F# release, several noteworthy features have been introduced to enhance the language's expressiveness and efficiency. One significant addition is the shorthand for defining simple lambda functions, which proves particularly useful for situations where a lambda function is confined to an atomic expression on the lambda argument. This shorthand, expressed as _.Property shorthand for (fun x -> x.Property), simplifies the code, making it more concise and readable.

A second key feature addresses the copy-and-update functionality for nested records. This enhancement simplifies the process of copying and updating nested record fields, providing developers with a more efficient and straightforward approach to field manipulation within records.

The while! (while bang) feature, initially announced in an earlier preview, has been officially integrated. This feature simplifies the use of computation expressions, especially when looping over a boolean condition that needs evaluation within the computation expression, such as within an async{} block.

let doStuffWithWhileBang =
    async {
        while! asyncCondition do
            count <- count + 2
        return count

F# 8 also introduces an extended string interpolation syntax, drawing inspiration from C#. This improvement enhances the support for existing interpolated strings in F#. Literal text output can now seamlessly combine with values and expressions by enclosing them in braces {}. However, to use braces as literal parts of the output, they need to be escaped by doubling (via {{ and }}). As stated, this can be helpful when dealing with texts containing numerous braces, such as when embedding other languages in F# strings, like JSON, CSS, or HTML-based templating languages like mustache.

Furthermore, another significant update involves string literals used in built-in printing functions (printfn, sprintfn, etc.). In previous versions, the specified format had to be a string literal typed directly at the usage location. With F# 8, string literals defined elsewhere are now supported. Additionally, developers can define a string literal using a concatenation of existing string literals. This allows for the reuse of commonly repeated format specifiers as patterns, reducing redundancy in the codebase.

let formatBody = "(%f,%f)"
let formatPrefix = "Person at coordinates"
let fullFormat = formatPrefix + formatBody

let renderedCoordinates = sprintf formatBody 0.25 0.75
let renderedText = sprintf fullFormat 0.25 0.75

There are some other changes also, including arithmetic operators in literals, type constraint intersection syntax, extended fixed bindings, simplified [<Extension>] method definitions and others. The release also includes the changes related to new and improved diagnostics.

With this release, F# is experiencing significant enhancements to improve uniformity and consistency. Static members can now be declared and implemented in interfaces, emphasizing concrete members in contrast to F#7's static abstract members. Additionally, static bindings, including static let and static do, are extended to discriminated unions, records, structs, and types without primary constructors, promoting the encapsulation of data and logic within type definitions.

type IDemoable =
    abstract member Show: string -> unit
    static member AutoFormat(a) = sprintf "%A" a

Also, the try-with code construct is now supported within collection builders, such as seq{} for IEnumerable definitions, [] for list builders, and [||] for array builders. This extension enables exception handling within these expressions, allowing for various combinations of 'try' and 'with' scenarios, enhancing the overall clarity and conciseness of F# code.

F# 8 introduces several quality-of-life improvements. One significant improvement is the introduction of trimmability for compiler-generated code, enhancing code clarity and reducing unnecessary elements.

Parser recovery has been refined, contributing to a more robust and resilient parsing process for improved code comprehension. Strict indentation rules have been implemented, promoting consistent code formatting and enhancing readability across F# projects.

Autocomplete functionality has been refined, providing an improved developer experience by offering more accurate and context-aware suggestions during coding. Additionally, [<Struct>] unions now support over 49 cases, expanding the versatility of this construct within F#.

Compiler performance has been a focal point in this release, addressing crucial aspects related to the F# compiler and associated tooling. Special attention has been given to two key areas F# – incremental builds of large graphs of projects via the Reference assemblies feature, and CPU-parallelization of the compiler process.

Overall community feedback about this release was positive, and a couple of community members wrote that they already experienced the performance improvements. A user called David N wrote the following comment on the original blog post announcement:

Brilliant work. It’s taken me a while to get to read this and I see I have several things to go and try out now. Especially the new experimental compiler optimisations. I had already been trying the graph optimisations and they gave a 20% improvement to my project build times just with that one feature.

Finally, the Microsoft team has published the release announcement in a detailed blog post highlighting all the new features of F# 8.0. Readers are strongly encouraged to dive into the post for a comprehensive exploration of these updates.

About the Author

Rate this Article