6.9.9. Multiline string literals¶
- MultilineStrings¶
- Since:
9.12.1
Enable multiline string literals.
With this extension, GHC now recognizes multiline string literals with """
delimiters. Indentation is automatically stripped, and gets desugared to normal string literals, so it works as expected for OverloadedStrings
and any other functionality. The indentation that is stripped can be informally defined as “The longest prefix of whitespace shared by all lines in the string, excluding the first line and any whitespace-only lines”.
Normal string literals are lexed, then string gaps are collapsed, then escape characters are resolved. Multiline string literals add the following post-processing steps between collapsing string gaps and resolving escape characters:
Split the string by newline characters
Includes
\r\n
,\r
,\n
,\f
Replace leading tabs with spaces up to the next tab stop
Remove common whitespace prefix in every line
If a line only contains whitespace, remove all of the whitespace
Join the string back with
\n
delimitersIf the first character of the string is
\n
, remove itIf the last character of the string is
\n
, remove it
6.9.9.1. Examples¶
Expression |
Output |
Notes |
---|---|---|
"""
Line 1
Line 2
Line 3
"""
|
"Line 1\n"
++ "Line 2\n"
++ "Line 3\n"
|
|
"""Test
Line 1
Line 2
Line 3
"""
|
"Test\n"
++ "Line 1\n"
++ "Line 2\n"
++ "Line 3\n"
|
Characters on the same line as the delimiter are still included |
"""
Line 1
Line 2
Line 3\
\"""
|
"Line 1\n"
++ "Line 2\n"
++ "Line 3"
|
Omit the trailing newline with string gaps |
"""
"Hello"
\"\"\"
\"""
"""
|
"\"Hello\"\n"
++ "\"\"\"\n"
++ "\"\"\"\n"
|
Double quotes don’t need to be escaped unless they’re triple quoted |
"""
<div>
<p>ABC</p>
</div>
"""
|
"<div>\n"
++ " <p>ABC</p>\n"
++ "</div>\n"
|
Only common indentation is stripped |
"""
\& Line 1
\& Line 2
\& Line 3
"""
|
" Line 1\n"
++ " Line 2\n"
++ " Line 3\n"
|
Use |