[swift-evolution] multi-line string literals.

Rainer Brockerhoff rainer at brockerhoff.net
Wed Apr 27 08:08:45 CDT 2016


On 4/27/16 05:52, Brent Royal-Gordon via swift-evolution wrote:
>  <https://gist.github.com/brentdax/c580bae68990b160645c030b2d0d1a8f#proposed-solution>Proposed solution
> 
> We propose that, when Swift is parsing a string literal, if it reaches the end of the line without encountering an end quote, it should look at the next line. If it sees a quote mark there (a "continuation quote"), the string literal contains a newline and then continues on that line. Otherwise, the string literal is unterminated and syntactically invalid.
> 
> Our sample above could thus be written as:
> 
> let xml = "<?xml version=\"1.0\"?>
>           "<catalog>
>           " <book id=\"bk101\" empty=\"\">
>           "     <author>\(author)</author>
>           " </book>
>           "</catalog>"
> (Note that GitHub is applying incorrect syntax highlighting to this code sample, because it's applying Swift 2 rules.)
> 
> This format's unbalanced quotes might strike some programmers as strange, but it attempts to mimic the way multiple lines are quoted in English prose. As an English Stack Exchange answer illustrates <http://english.stackexchange.com/a/96613/64636>:

+1 from me. This looks like a good compromise and the analogy to English
is good (though maybe less intuitive for non-native speakers).

I've seen objections that it would require significant work to "just"
paste in, say, a large XML file as a literal, and go back adding quotes
on every line. My reaction was that this is comparatively rare and
perhaps even should not be made overly easy :-).

On the other hand, Xcode (for one) already has a "paste and preserve
formatting" command. This could be extended to work as "paste as
multi-line string literal" under appropriate conditions, inserting
quotes after newlines, etc.

-- 
Rainer Brockerhoff  <rainer at brockerhoff.net>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
http://brockerhoff.net/blog/



More information about the swift-evolution mailing list