Search code examples
f#string-interpolation

Embedded Strings in Interpolated Strings in F#


I cannot seem to figure out how I can embed strings inside an interpolated string in F#. Superficially, it is very similar to C#. For example, in C# I would write something like

Console.WriteLine($"Truly, it is {(string.IsNullOrEmpty("") ? "" : "not ")}empty.");

//Result: Truly, it is empty.   OR   Truly, it is not empty.

The parentheses inside the curly brackets allow me to embed additional string literals within the interpolation expression.

I have attempted to replicate this in F# with

printfn $"Truly it is {(match (String.IsNullOrEmpty "l") with | true -> "" | false -> "not ")}empty."

The parentheses wrapping the interpolated expression do not accomplish the same thing, it seems. It suggests using an explicit let binding for the interpolated match expression, but for learning purposes I want to do it in a single line.

Is there a way to do this in F#, or am I stuck defining a let for the embedded ternary?


Solution

  • Use triple-quoted strings:

    printfn $"""Truly it is {match (String.IsNullOrEmpty "l") with | true -> "" | false -> "not "}empty."""