[swift-evolution] multi-line string literals.
brent at architechies.com
Mon May 2 07:53:40 CDT 2016
> So I’m pretty late to the discussion here, but my question is; do we really want to encourage large blocks of text in code?
It's not written up, but it has occurred to me that, rather than having `"""` strings or even heredocs for large chunks of text, we might want something which puts the text at the end of the file, or even in a separate file. I even have some vague ideas about what that might look like.
But that is so *massively* out of scope for Swift 3 that I don't think it's worth contemplating right now. There are so many interesting possibilities we virtually have to punt.
The latest draft of my proposal adds this section:
### An aside: Small and large multiline strings
The first entry in the list above, "Putting newlines in string
literals", might be thought of as a subset of the fourth, "putting very
large quantities of text in string literals". (In fact, *all* of the
entries could perhaps be subsumed by the fourth.) However, we believe
they are best addressed as separate goals, using separate features.
When you are embedding enormous string literals in source code, you
must put undistorted representation of the string above all other
considerations. If the design which best permits the string to be
written verbatim is ugly, bulky, unlike other language constructs,
disruptive to code readability, error-prone, arbitrary, difficult to
parse, or otherwise a wart on the language, that is simply the price we
have to pay for that feature.
But it's a different story for short multiline strings. When you are
writing a little bit of text, but still more than one line, you don't
want to disrupt your code's indentation, add whole lines just for
delimiters, insert bizarre or cryptic tokens into your code, or create
syntax errors which take ten minutes to trace back to their source. You
want a different feature, with different tradeoffs.
It is that feature which this proposal seeks to design.
I envision that, during Swift 3, people will indeed use multiline string literals for very long strings. But eventually (hopefully by 4), we will have some other feature specifically designed and optimized for that task, and multiline string literals will only be used for relatively short strings where the "really long string" feature would be too clunky. I mean things like error messages, short snippets of HTML, stuff like that. Things you write in your source code, rather than designing somewhere else and then copying to your source code.
More information about the swift-evolution