fsharp / fsharp/fslang-suggestions

Longer quoted strings

Open
#1,301 7 comments 4 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

**I propose we** add longer quoted strings (7+ quotes since 6 quotes specify an empty triple-quoted string) for when we need to embed source code that contains triple quoted strings. It is in line with C#'s design of [raw string literals with variable length of starting quotes](https://github.com/dotnet/csharplang/blob/345fa10e4f19b5510b60cbc4822ad953213a906f/proposals/csharp-11.0/raw-string-literal.md). Here, we can follow C#'s design of the multiline literal with the end delimiter specifying the indentation size (must be whitespaces, not any other character):

```fs
let fsharpCode = """""""
let string = $"""{String.replicate 3 "Hello World!\n"}"""
"""""""
let fsharpCode' = """""""
let string = $"""{String.replicate 3 "Hello World!\n"}"""
"""""""
fsharpCode = fsharpCode' // true
```

The newlines directly next to the delimiters will not be included in the string. Therefore, the string has the following content without preceding or succeeding newlines:
```fs
let string = $"""{String.replicate 3 "Hello World!\n"}"""
```

Since 7 quotes are already quite long, we won't allow single-line versions of it. So these are illegal:

```fs
let fsharpCode = """""""let string = $"""{String.replicate 3 "Hello World!\n"}""" """"""" // error - no single line version
let fsharpCode' = """""""
let string = $"""{String.replicate 3 "Hello World!\n"}""" """"""" // error - ending delimiter must be on its own line
let fsharpCode'' = """""""let
string = $"""{String.replicate 3 "Hello World!\n"}"""
""""""" // error - starting delimiter must be followed by a newline
```

The minimum lines that a longer quoted string must occupy is 3:
```fs
let empty = """""""
""""""" // error - too short - leave some space for adding content!
let empty' = """""""

""""""" // ok
```
This is because the starting delimiter requires a newline after, and the ending delimiter requires a newline before.

Interpolation will work:
```fs
let fsharpCode = $$"""""""
let string = $"""{String.replicate %%d{{3}} "Hello World!\n"}"""
"""""""
```
and produces the same string as before.
**The existing way of approaching this problem in F# is** to use runtime string manipulation which is less visually appealing and costs performance.

## Pros and Cons

**The advantages of making this adjustment to F# are**
1. Alignment with C#
2. Convenience
3. Visual appeal compared to triple quoted strings

**The disadvantage of making this adjustment to F# is** the learning complexity and implementation and maintenance costs.

## Extra information

**Estimated cost (XS, S, M, L, XL, XXL):** S to M

**Related suggestions:**
- https://github.com/dotnet/csharplang/issues/4304

## Affidavit (please submit!)

Please tick these items by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [StackOverflow](http://stackoverflow.com)) and I have searched StackOverflow for discussions of this issue
* [x] This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to [the compiler and tooling repository](https://github.com/dotnet/fsharp)
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate

Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this

## For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the proposed multiline examples and the related C# raw string literal issue linked in the report. Check the delimiter, indentation, interpolation, newline, and minimum-length rules described here; done means the examples behave as specified, including the listed invalid forms being rejected.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.