[swift-evolution] [Review] SE-0168: Multi-Line String Literals

Brent Royal-Gordon brent at architechies.com
Thu Apr 13 04:03:29 CDT 2017


> On Apr 12, 2017, at 5:39 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> 
> On Wed, Apr 12, 2017 at 5:20 PM, Brent Royal-Gordon <brent at architechies.com <mailto:brent at architechies.com>> wrote:
> Wow, maybe I shouldn't have slept.
> 
> Okay, let's deal with trailing newline first. I'm *very* confident that trailing newlines should be kept by default. This opinion comes from lots of practical experience with multiline string features in other languages. In practice, if you're generating files in a line-oriented way, you're usually generating them a line at a time. It's pretty rare that you want to generate half a line and then add more to it in another statement; it's more likely you'll interpolate the data. I'm not saying it doesn't happen, of course, but it happens a lot less often than you would think just sitting by the fire, drinking whiskey and musing over strings.
> 
> I know that, if you're pushing for this feature, it's not satisfying to have the answer be "trust me, it's not what you want". But trust me, it's not what you want.
> 
> This is not a very good argument. If you are generating files in a line-oriented way, it is the function _emitting_ the string that handles the line-orientedness, not the string itself. That is the example set by `print()`:
> 
> ```
> print("Hello, world!") // Emits "Hello, world!\n"
> ```

You say "this is the example set by `print`", but I don't think anything else actually *follows* that example. No other I/O operation in Swift behaves this way. The underlying `TextOutputStream.write(_:)` doesn't; the I/O in Foundation doesn't; file descriptor I/O doesn't. Concatenation certainly doesn't; nor does anything else you might do to assemble several multiline string literals into a whole. So I think `print()` is the exception here, not the rule.

In my opinion, modulo the "newline after leading delimiter" question, if code like this example:

	var xml = """
	    <?xml version="1.0"?>
	    <catalog>
	    """
	
	for (id, author, title, genre, price) in bookTuples {
	    xml += """
	            <book id="bk\(id)">
	                <author>\(author)</author>
	                <title>\(title)</title>
	                <genre>\(genre)</genre>
	                <price>\(price)</price>
	            </book>
	        """
	}
	
	xml += """
	    </catalog>
	    """

Doesn't assemble the kind of string that it's blatantly obvious the user is trying to assemble, I think the syntax has failed.

`print()` is the outlier. It's an important enough outlier that we should probably help the user notice when they're about to get it wrong, but an outlier it is.

-- 
Brent Royal-Gordon
Architechies

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170413/39576731/attachment.html>


More information about the swift-evolution mailing list