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

Adrian Zubarev adrian.zubarev at devandartist.com
Thu Apr 13 05:00:04 CDT 2017


Assuming the multi-line string literal to behave like I summarized it in my last post and that the for loop runs two times, we’d have to alter your example a little, which will then feel natural for concatenation.

I use tabs for ident.

// Notice the blank line at the end, which does  
// not inject a new line after itself
var xml = """
    <?xml version="1.0"?>
    <catalog>
     
    """
     
for (id, author, title, genre, price) in bookTuples {
     
    // Same here, a blank line at the end which does not
    // inject a new line after itself. It's natural for  
    // concatenation.
    xml += """
            <book id="bk\(id)">
                <author>\(author)</author>
                <title>\(title)</title>
                <genre>\(genre)</genre>
                <price>\(price)</price>
            </book>
         
        """
}

xml += """
    </catalog>
    """
You could also write \n or \n\ instead of an explicit new line, but I think it’s natural enough that way, because now the reader of the code sees that the next concatenation will happen on the blank line (equivalent to "" + "string from the next iteration").

The result:

<?xml version="1.0"?>
<catalog>
    <book id="bk1">
        <author>A</author>
        <title>B</title>
        <genre>D</genre>
        <price>42</price>
    </book>
    <book id="bk2">
        <author>A</author>
        <title>C</title>
        <genre>D</genre>
        <price>42</price>
    </book>
</catalog>   


-- 
Adrian Zubarev
Sent with Airmail

Am 13. April 2017 um 11:03:33, Brent Royal-Gordon (brent at architechies.com) schrieb:

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>
    """
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170413/3df9f10e/attachment.html>


More information about the swift-evolution mailing list