[swift-evolution] multi-line string literals.

Sean Heber sean at fifthace.com
Thu May 5 16:45:39 CDT 2016


I admit I have not been following this closely and have not read all proposals and counter proposals and concerns - but of course that isn’t going to stop me from tossing out my own suggestion!

My idea starts with this:

let code = #string // some code that we are generating
         + #string and some more code here
         + #string another line here
         + #string
         + #string including a blank line if we wanted
         + #string etc.

When used in this way, it simply generates a string literal from everything that follows “#string” up to and including the newline. To chain multiple lines together, it just uses string’s normal + operator. This would be the same as writing the following code:

let code = "// some code that we are generating\n"
         + "and some more code here\n"
         + "another line here\n"
         + "\n"
         + "including a blank line if we wanted\n"
         + "etc.\n"

I would then extend this to support some other useful approaches such as:

#string(THE_END)
blah blah blah   \\\   blah
  can  be anything \o/
what    ever!!
THE_END

Or to use the entire contents of an external file:

#string(“file.txt")

l8r
Sean





> On May 5, 2016, at 3:51 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> It had the idea that maybe the motivation behind continuation quotes is to make using multiline strings harder to use; in order to discourage their use at all.
> 
> Absolutely not, and frankly I resent the implication.
> 
> I'm looking at a space where (in my opinion) all existing designs are ugly and inelegant and trying to find something better. There are *reasons* Swift 2 doesn't allow newlines in quote marks; I'm trying to address them, not ignore them. There are nasty formatting problems; I'm trying to solve them, not tolerate them. There are significant context-sensitivity issues; I'm trying to fix them, not punt the problem to a syntax highlighter that is absent or incorrect in half of our tools.
> 
> Here's a piece of code-generating code I once wrote, ported to this prototype:
> 
> 	https://gist.github.com/brentdax/8a8ee1f6028230f68ed85b7f3ebc95bf
> 
> (The actual code generation starts on line 93.) 
> 
> And here's the same code in (beta-era) Swift 2:
> 
> 	https://github.com/brentdax/WebResponder/blob/master/WebResponderCore/WebResponderCore/codes2swift.swift
> 
> I think the new version is an improvement over the old. I think I like it better than I would with `"""` or heredocs forcing things against the left margin. I think it's nice that I can look at any line—out of context, random access—and see which parts are string literals. I think if I want to add a doc comment to each of the constants it generates, I have to do a little awkward code shuffling, but the result is worth it:
> 
> 	for statusLine in statusLines {
> 	    code +=  "    /// The HTTP \(statusLine.code) \(statusLine.message) status, used with `HTTPResponseType.status`.
> 	             "    \(statusLine.constant)
> 	             "    
> 	             ""
> 	}
> 
> Overall, I find this code a lot more pleasant to read than it was in Swift 2, and somewhat more pleasant than it would be with `"""` or a heredoc. Maybe you don't agree; maybe you'd rather import some other language's warts. I understand—those warts come with convenience advantages, and you might prefer that convenience to the other things.
> 
> But at least have the basic respect for opposing viewpoints not to impugn my motives because you disagree with me or don't like what I suggest. That is just not nice.
> 
> -- 
> Brent Royal-Gordon
> Architechies
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list