Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indentation aware raw string literals (similar to C# 11) #13995

Closed
stefan-schweiger opened this issue Sep 27, 2022 · 1 comment
Closed

Indentation aware raw string literals (similar to C# 11) #13995

stefan-schweiger opened this issue Sep 27, 2022 · 1 comment

Comments

@stefan-schweiger
Copy link

Is your feature request related to a problem? Please describe.

Currently you have to be careful about the indentation of multiline strings in your code

let htmlString1 = """<!DOCTYPE html>
<html lang="en">
    <head>
    </head>
    <body>
        <h1>Hello</h1>
     </body>
</html>"""

let htmlString2 = """
    <!DOCTYPE html>
    <html lang="en">
        <head>
        </head>
        <body>
            <h1>Hello</h1>
         </body>
    </html>
    """

printfn $"%b{htmlString1 = htmlString2}" // -> false

Describe the solution you'd like

With C# 11 raw string literals were introduced which remove indentation depending of the position of the closing """ and ignores leading and trailing new lines.

E.g.

var htmlString1 = """
    <!DOCTYPE html>
    <html lang="en">
        <head>
        </head>
        <body>
            <h1>Hello</h1>
         </body>
    </html>
    """;

var htmlString1 = """
<!DOCTYPE html>
<html lang="en">
    <head>
    </head>
    <body>
        <h1>Hello</h1>
     </body>
</html>
""";

Console.WriteLine(htmlString1 == htmlString2); // -> true

F# should support this as well, maybe prefixed with @:

let htmlString1 = @"""<!DOCTYPE html>
<html lang="en">
    <head>
    </head>
    <body>
        <h1>Hello</h1>
     </body>
</html>"""

let htmlString2 = @"""
    <!DOCTYPE html>
    <html lang="en">
        <head>
        </head>
        <body>
            <h1>Hello</h1>
         </body>
    </html>
    """

printfn $"%b{htmlString1 = htmlString2}" // -> true

Describe alternatives you've considered

You can create a function which looks at the number of whitespaces on the last line and removes the same number of leading whitespaces from all other lines. But it's very easy to use wrong (for example on strings which where not written as multiline in your code, but just contain "\n") and you won't get any compiler errors/warnings if your lines misalign.

@vzarytovskii
Copy link
Member

@stefan-schweiger I think it is covered by fsharp/fslang-suggestions#1150
Does it match what you want? If no, please create another proposal in the https://github.com/fsharp/fslang-suggestions/issues

@vzarytovskii vzarytovskii closed this as not planned Won't fix, can't repro, duplicate, stale Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants