Microsoft previews new F# syntax for string interpolation

Improved syntax makes it easier to work with text like CSS that contains many curly braces, while maintaining backwards compatibility with F#’s triple-quoted strings.

young woman holding string of lights innovation creativity digital transformation by matheus bertel
Matheus Bertelli (CC0)

Microsoft is previewing new syntax for string interpolation in F#, an improvement intended to make it easier to work with interpolated strings.

Interpolated strings allow developers to embed F# expressions into string literals. But they can become cumbersome when dealing with text that contains many curly braces. The F# interpolation syntax addresses this. Announced July 27 and modeled after how interpolation works in C# raw strings, the new syntax maintains backward compatibility with F#’s triple-quoted strings.

The new syntax is an extension of the existing F# syntax for interpolated strings. Developers can use multiple $ characters and corresponding numbers of opening and closing curly braces for interpolation. The same rules apply to % characters, which have special meaning in F# interpolated strings as format specifiers.

An example of where this new syntax can be useful is when working with CSS literals in a front-end F# application, such as with the Fable F# compiler for JavaScript. The new syntax lets developers write CSS without having to worry about escaping curly braces. Instead, developers can focus on the interpolation expressions. To try out this new feature, developers must use the flag --langversion:preview.

F# is a language that offers first-class functions, lightweight syntax, pattern matching, and async programming, according to Microsoft. It is immutable by default. Other recent improvements for F# developers include a preview of F# type and parameter hints in Visual Studio. Installation instructions for the language can be found on learn.microsoft.com.

Copyright © 2023 IDG Communications, Inc.