9

I'm a C# developer who try to learn F#.

As far as I know, F# 2.0 had two kind of syntaxes for strings: normal strings, and verbatim strings (like C#). With F# 3.0 version there is a feature called tripled-quoted strings.

As far as I see, with this string format, every thing between """ is a verbatim string literal. And there is no need to escape escapse sequence characters like double quotes.

For example all these are valid strings;

let a = """ This is a valid "string" """
let b = """ This is a valid \string """
let c = """ This is a valid 'string """

But there is a rule with it;

Quotes in the triple-quoted string cannot end with a double-quote (“), but it can begin with one.

So this is a legal string;

let s = """"This is a valid string"""

but this is not;

let s = """This is a valid string""""

Why is that? I looked at Strings (F#) on MSDN page, F# 3.0 Language Spec $3.5 Strings and Characters part and More About F# 3.0 Language Features but I couldn't find any information about why it's legal to use in the begining of string but not at the end.

Can you enlighten me?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

2 Answers2

8

The answer is simple: the triple-quoted string ends as soon as the compiler sees three quotes. So """a"""" is a string constisting of the character a, followed by an extra ", which starts a new string.

If you want to write obfuscated code, you might do something like:

f"""a""""b"

To call the function f with two string "a" and "b".

pdw
  • 8,359
  • 2
  • 29
  • 41
  • _Well_, your answer sounds logical in my opinion. Upvoted. But why compiler doesn't consider the last three quotes instead of first three? Because at the start of string, compiler considers first three quotes. The quotes after this part, compiler consider them as a part of string. For example; `""""""a"""` could be empty string because there are 3 quotes after 3 quotes. And when compiler see this 3 quotes, it might say "_Okey, let's end this string_". At least it should give compile time error. But it is a valid string. That confuses me. – Soner Gönül Jul 06 '14 at 12:52
  • 3
    @SonerGönül: Actually `""""""a"""` does give a compile time error. As you correctly expected, a triple-quoted string can start with one or two quotes, but not three. – Tarmil Jul 07 '14 at 13:09
0

Hacky workaround that uses string interpolation (and therefore requires F# 5+):

let doubleQuote = '"'
let x = $"""User said "something{doubleQuote}"""
printf "%s" x
Ignore the syntax highlighting, SO can't handle `"""`

Ignore the above. Bent's suggestion below of + is clearer and has better runtime characteristics.

DharmaTurtle
  • 6,858
  • 6
  • 38
  • 52
  • The question was not about how to work around the limitation, but why it's there in the first place. If you want a quote at the end, the simplest is perhaps just to add it to the original string with the plus operator. At best the compiler might then perhaps even do it at compile-time, and avoid unnecessary runtime instructions. – Bent Tranberg Jan 30 '22 at 16:39