You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This proposal introduces the ability to escape newlines in single and multi-line strings to improve readability and maintenance of source material containing excessively long lines.
13
-
14
-
This proposal adds onto [SE-0168](0168-multi-line-string-literals.md).
12
+
This proposal is a refinement of [SE-0168](0168-multi-line-string-literals.md) which introduces the ability to escape newlines in single and multi-line strings to improve readability and maintenance of source material containing excessively long lines.
@@ -41,7 +39,7 @@ Incorporating a string continuation character is well founded, used in other dev
41
39
42
40
## Detailed design
43
41
44
-
This proposal introduces `\` as a line continuation character which escapes newlines matching the following regular-expression: `/\\[ \t]*\n/`. In other terms, line continuation requires a `\` character, followed by zero or more whitespace characters, followed by a newline character. All those characters are omitted from the resulting string.
42
+
This proposal introduces `\` as a line continuation character which escapes newlines matching the following regular-expression: `/\\[ \t]*\n/`. In other terms, line continuation requires a `\` character, followed by zero or more horizontal whitespace characters, followed by a newline character. All those characters are omitted from the resulting string.
45
43
46
44
As a consequence, these rules follow:
47
45
@@ -69,43 +67,9 @@ assert(str1 == str2)
69
67
70
68
This does not affect the indentation removal feature of multiline strings and does not suggest that indentation removal should be added to conventional strings but it does give them consistent treatment.
71
69
72
-
## Further discussions
73
-
74
-
The following topics are related to escaping newlines. If newline-escaping where accepted it may be appropriate to revisit these decisions when considering this proposal though that shouldn't be the focus of the discussion.
75
-
76
-
### Reconsidering stripping the last newline in multi-line strings
77
-
78
-
When SE-0168 was reviewed, it was decided to strip the last newline in multi-line strings. Doing the opposite would have been ill-advised without a line continuation character to escape it when necessary. If this proposal is accepted, it might be worth reconsidering this decision and include the final newline in the literal. For example, it would allow easier concatenation of multi-line strings:
79
-
80
-
```swift
81
-
var xml ="""
82
-
<?xml version="1.0"?>
83
-
<catalog>
84
-
"""
85
-
86
-
for (id, author, title, genre, price) in bookTuples {
87
-
xml +="""
88
-
<book id="bk\(id)">
89
-
<author>\(author)</author>
90
-
<title>\(title)</title>
91
-
<genre>\(genre)</genre>
92
-
<price>\(price)</price>
93
-
</book>
94
-
"""
95
-
}
96
-
97
-
xml +="""
98
-
</catalog>
99
-
"""
100
-
```
101
-
102
-
### Warning about trailing whitespace in multi-line strings
103
-
104
-
During the implementation of SE-0168, it was decided not to warn about trailing whitespace in multi-line strings. One of the reasons brought up was that the only way to silence the warning was with a no-op character sequence at the end of the line; the only option back then was `\("")`, which is less than ideal. With this proposal, a slightly more elegant solution is now available: `\n\`.
105
-
106
70
## Source compatibility
107
71
108
-
As this proposal is additive proposing a syntax that is not currently allowed in Swift this does not affect existing source.
72
+
This proposal does not affect existing source, because it is purely additive - enabling syntax that is not currently allowed in Swift.
109
73
110
74
## Effect on ABI stability
111
75
@@ -117,7 +81,7 @@ This proposal does not affect ABI resilience.
117
81
118
82
## Alternatives considered
119
83
120
-
It has been heavily debated between the authors of the proposals wether newline escaping should be supported in single-line strings. One argument against it is that the lack of indentation stripping in single-line strings forces strings to include no indentation, hindering the readability of code by visually breaking scopes when returning the column 1:
84
+
It has been heavily debated between the authors of the proposals whether newline escaping should be supported in single-line strings. One argument against it is that the lack of indentation stripping in single-line strings forces strings to include no indentation, hindering the readability of code by visually breaking scopes when returning the column 1:
121
85
122
86
```swift
123
87
classMessager {
@@ -133,4 +97,9 @@ empty message, it has no meaning.")
133
97
}
134
98
```
135
99
136
-
Another counter-argument is that further proposals might come up to fix this problem by introducing indentation stripping to single-line strings, muddying the distinction between single and multi-line strings.
100
+
Another argument against it is that lines that are sufficiently long and/or complex could use
101
+
multi-line string literals with newline escaping, so there is no need to include them in single
102
+
quoted strings.
103
+
104
+
A counter-argument is that it is important to keep single and triple quoted strings consistent
0 commit comments