-1

I expected string interpolation to be the exact same for F# and C#.

This is fine for C#:

var x = $"{123:00000000}";

This does not work for F#:

printfn $"{123:00000000}"

Looking for an explanation.

I would expect MS docs to have a "F#/C# interpolation differences" but it ain't there https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/interpolated-strings

asked Mar 7, 2025 at 8:12
5
  • 1
    "I expected string interpolation to be the exact same for F# and C#." They're just not. Even a quick look at the syntax of the F# interpolated strings shows %format-specifier which wouldn't be present in C#. I would say it's very rarely a good idea to expect anything to work exactly the same between two languages. Commented Mar 7, 2025 at 8:25
  • 1
    "Looking for an explanation." You've linked to the documentation, which includes multiple examples of using format specifiers. That is the explanation. I'm not sure what you'd expect to see in an answer that isn't in the docs. Commented Mar 7, 2025 at 8:26
  • 2
    F# and C#, while both running on .NET and both being developed and maintained by Microsoft, are two different languages. Expecting things to be the exact same between F# and C# is like expecting things to be the exact same between Java and Scala. Why would Microsoft write up a single page on how string interpolation works in C# and in F# highlighting the differences, if instead they can simply write one page for C#, one for F# and let the reader grasp the obvious differences between them? Commented Mar 7, 2025 at 8:39
  • 1
    If you scroll down to the Format Specifiers section, it shows that for custom formats, you need to quote them in backticks `` ``. Commented Mar 7, 2025 at 8:40
  • Thank you for the comments. I will read the docs more diligently. Commented Mar 7, 2025 at 9:37

2 Answers 2

5

Does string interpolation work the same for C# and F#?

No! Or rather, "not quite".

This is covered by the documentation.

Short version, if the format specifier contains "unusual characters" then it needs to be quoted with double back ticks.

Eg. (from the docs)

let nowDashes = $"{System.DateTime.UtcNow:``yyyy-MM-dd``}" // e.g. "2022年02月10日"

For completeness: F# supports three types of string formatting

  • Plain text formatting: inbuilt, type-safe (checked by the compiler and tools). There are many methods supporting this (in F#'s core library's Printf Module).
  • String Interpolation: as above. Something of a hybrid of C#'s interpolated strings and F#'s plain text formatting.
  • You can of course call String.Format (et. al.) directly.
answered Mar 7, 2025 at 9:37
Sign up to request clarification or add additional context in comments.

Comments

1

F# supports string interpolation, but it does not use string.format internally. Use printfn $"{123.ToString("00000000")}" instead, beacuase F# needs a more explicit conversion. Hope this helps

answered Mar 7, 2025 at 8:27

9 Comments

Any reason not to use the .NET-style format specifiers as per learn.microsoft.com/en-us/dotnet/fsharp/language-reference/…? (That certainly gives the impression that they can be used - and I'd expect that those format specifiers would end up with a call to string.Format.)
For example, printfn $"{123:``00000000``}" seems to do the right thing...
It can be used, but does only work under certain conditions. The thing is, F#'s string interpolation ($"...") does not support :format specifiers directly for some reason. I think that may be the cause of the problem
That seems to contradict the documentation. Can you give a concrete example of a format specifier that doesn't work? I'd like to know more.
@JonSkeet one case is the %A formatting (eg. $"%A{myRecord}" which will perform structured plain text formatting, which – without ToString overrides – will give details of the instance (rather than just the type name).
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.