[swift-evolution] multi-line string literals

Travis Tilley ttilley at gmail.com
Fri Dec 11 01:27:52 CST 2015


On Fri, Dec 11, 2015 at 2:06 AM, Drew Crawford via swift-evolution <
swift-evolution at swift.org> wrote:

>
> tl;dr: I have two questions.
>
> 1.  Are multi-line strings more commonly used as “long single-line
> strings” (e.g. wants to be escaped) or “pasted data” (e.g. wants to be
> non-escaped)?  How can we find out?
> 2.  For what answers of 1 is it sensible to make the user ALWAYS be
> explicit about what behavior they want?
>


​I think there are many more use cases than you suggest in #1. In my
primary use case of Docopt, which creates a command-line interface based on
provided help text, an example multi-line string would be this:

let helptext: String = """
​
Naval Fate.

Usage:
  naval_fate ship new <name>...
  naval_fate ship <name> move <x> <y> [--speed=<kn>]
  naval_fate ship shoot <x> <y>
  naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate -h | --help
  naval_fate --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.
​"""


>From that string, docopt will generate an appropriate CLI... properly
handling arguments, options, and defaults. Also, if you call the binary
with -h or --help, this text will be output as-is. No escape sequences or
interpolation are used here, but that doesn't mean that they shouldn't be
(replacing a few names with the actual binary name might be handy, for
example).

As for your question #2... I believe that the sane option should always be
the easiest, and the less sane option should be possible, but with
"syntactic vinegar" to make it less pleasant to use. In this specific
instance, however, I'm not sure what that would entail. ^_^;

Please give me more info on what would make your specific use case less
unpleasant. JSON/JavaScript escape sequences tend to correspond pretty well
with swift (with notable exceptions).


- Travis Tilley
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/681cb304/attachment.html>


More information about the swift-evolution mailing list