Triple-quote string C# vs F# heterogeneous behaviour

98 Views Asked by At

In C# the triple-quote string, or verbatim string behaves a little different from how F# behaves.

In C#:

{ // just for indentation
    var message = """
    this is a message from C#
    """;
    Console.WriteLine($"->{message}<-");
}
dotnet run

prints

->this is a message from C#<-

In F#

open System

module BasicFunctions =

    let message = """
    this is a message from F#
    """
    printfn $"->{message}<-"
dotnet run

prints

->
    this is a message from F#
    <-

Is this intended? Am I missing something? Is this related to the influence that Python has over F#?

1

There are 1 best solutions below

1
Jim Foye On BEST ANSWER

The behavior you're seeing in C# is described here:

https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw-string-literals

Triple quoted strings appeared in F# first. The compiler incorporates everything between the pairs of triple quotes into the string. So sometimes you have to declare things such that the declaration is a bit less than elegant, to avoid including something unwanted in the string at runtime (like initial white space).

If I had to guess, I would say that when the the folks at Microsoft decided to add support for raw string literals in C# 11, they realized they could improve it a bit. Personally I think it's a good improvement, but it does mean the same raw literal string declaration may produce something different in the two languages.

Related Questions in F#