Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News F# 4.7 Enables Preview of New Language Features and Relaxes Syntax

F# 4.7 Enables Preview of New Language Features and Relaxes Syntax

The latest release of F# introduces a new LangVersion configuration option which makes it possible to choose which language version you would like the F# compiler to target. Additionally, it introduces a number of syntax relaxations that are aimed to remove a few idiosyncrasies of the language.

Starting with F# 4.7, developers can specify which language version they would like to use by including the LangVersion property in their projects. Most interestingly, this enables the possibility of working with preview versions of the language that are still considered experimental or have not reached yet their final design, which makes it easier for developers to play with them and provide feedback.

As a first iteration of this model of delivery of new features, F# 4.7 includes preview support for nameof and the ability to open static classes, which were among the features developers requested the most. nameof is a convenience function aimed to improve the logging of entity names such as parameters, functions, modules, etc. It enables using the actual name of the entity it is applied to without requiring to manually update it after a refactoring. The possibility of opening static classes aims to make static classes more flexible and brings their support in F# on a par with C#. Thanks to this feature, DSLs may benefit from a shortened syntax. For example, if you have a DSL implemented through a MyDSL static class or type provider exporting a static class, you can use unqualified names, e.g. feature instead of MyDSL.feature, by first opening the static class, just as you do with modules.

Still on the front of tooling, F# 4.7 enables F# Interactive from the .NET command line using dotnet fsi, although with some quirks related to package importing, and improves integration with Visual Studio 2019.

Speaking of syntax, as mentioned, F# 4.7 introduces a couple of syntax relaxations aimed at making the language friendlier. When specifying member declarations, previous versions of F# allowed you to omit this and replace it with a double underscore. F# 4.7 enables the use of a single underscore instead. Furthermore, parameters to constructors or static methods can be now freely indented, whereas previous versions of the compiler required them to be indented more deeply than the first parameter. Finally, for lists, arrays, sequences, and computations that define Yield, Combine, Delay, and Zero members, it is not required anymore to specify yield, so you can simply write the following:

let nums =
    seq {
        1    // implicitly assumes yield 1
        2    // implicitly assumes yield 2
        3    // implicitly assumes yield 3

let f' includeWeekend =
        if includeWeekend then  // conditional values required yield even in arrays - not anymore

F# 4.7 extends its support for parallel and asynchronous programming by adding a maxDegreesOfParallelism parameter to Async.Parallel and Async.Sequential to enable sequential execution of async computations.

Last but not least, F# 4.7 bumps up its support for .NET Standard to version 2.0. Thanks to this , F# 4.7 simplifies dependency management and supports a number of new APIs, including a few methods meant to simplify the handling of function values through FSharpFunc<'T, 'TResult> and Converter; and access to the MatchFailureException type and to the WebExtensions namespace.

To use F# 4.7, you need .NET Core 3.0and Visual Studio Code with Ionide or alternatively the latest Visual Studio.

Rate this Article