[swift-evolution] multi-line string literals.

Brent Royal-Gordon brent at architechies.com
Mon Apr 25 18:59:27 CDT 2016


> To audition Xcode using the following multi-line syntax:
> 
>         let xml = "<?xml version=\"1.0\"?>
>             "<catalog>
>             "   <book id=\"bk101\" empty=\"\">
>             "       <author>\(author)</author>
>             "       <title>XML Developer's Guide</title>
>             "       <genre>Computer</genre>
>             "       <price>44.95</price>
>             "       <publish_date>2000-10-01</publish_date>
>             "       <description>An in-depth look at creating applications with XML.</description>
>             "   </book>
>             "</catalog>
>             ""
>         print(xml)
> 
> You can install:  http://johnholdsworth.com/swift-LOCAL-2016-04-25-a-osx.tar.gz

This is super-cool, and in a little bit of testing, I really like what this does to multiline strings. The quotation marks on the left end up forming a column that marks the lines as special, much like using `///` on multiline doc comments instead of `/**`.

In the spirit of building prototypes, I wrote a Perl script which generates literals in various formats: <https://dl.dropboxusercontent.com/u/64017186/make-swift-literal.pl> It allows you to feed in different strings and evaluate how they might look with various language features we might add.

The command takes a file on the command line ("-" for stdin, nothing to use the contents of the pasteboard) and prints it as a Swift literal. It supports the following options:

* --multiline|nomultiline: Controls the hanging-quote format we've been discussing for multiline quotes.
* --unescaped|nounescaped: Controls the `e"string"` format we've been discussing for quotes that ignore escaping.
* --quotes=n: Sets the number of quotation marks to be used.
* --auto|noauto: If enabled, the exact settings are chosen heuristically, with the command-line options being used as maximums. If disabled, the options directly control the settings used.

The default is to use heuristics, enable all options with a very high maximum number of quotes, and read the string from the pasteboard. (These defaults are controlled from lines 9 and 13-16.) I chose these defaults so that this can be used to prototype a "Paste as String Literal" command in TextMate 2. Just open the Swift bundle in the bundle editor, paste the script into a new "Paste as String Literal" command, and choose "Key Equivalent: Ctrl-Cmd-V", "Input: Nothing", and "Output: Replace Selection".

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list