0

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#?

santiago arizti
  • 4,175
  • 3
  • 37
  • 50
  • 2
    I don't understand. What's so special about the same syntax behaving different in different languages? – Sweeper Mar 31 '23 at 00:40
  • both are modern languages, both are part of the .NET framework, and both support very similar syntax. For example, interpolation in js ```js `${foo}` ``` – santiago arizti Mar 31 '23 at 21:11

1 Answers1

4

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.

Jim Foye
  • 1,918
  • 1
  • 13
  • 15
  • 1
    good answer. Also, I noticed that in C# (by reading the link you provided) you can add extra `"` to allow for more flexibility and also if using string templated you can add extra `$$"""` to force the tpl to use `{{foo}}` which is awesome! this allows for nesting of string literals! – santiago arizti Mar 31 '23 at 20:48