2

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?

2
  • 2
    On a side note: the idiomatic way in F# is to use if then else instead of matching on a boolean. Commented Jan 19, 2021 at 6:27
  • 1
    Thank you. I'm just starting F# and still getting my legs. Commented Jan 19, 2021 at 16:37

1 Answer 1

5

Use triple-quoted strings:

printfn $"""Truly it is {match (String.IsNullOrEmpty "l") with | true -> "" | false -> "not "}empty."""
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.