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 ([email protected]) 
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>
    """
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to