[swift-evolution] multi-line string literals

John Siracusa siracusa at gmail.com
Fri Dec 11 11:01:52 CST 2015


Sorry about the lack of a subject on this message originally. I was
replying to the email digest and forgot to add one. It should be part of
the "multi-line string literals" thread.

-John

On Fri, Dec 11, 2015 at 11:52 AM, John Siracusa <siracusa at gmail.com> wrote:

>
> Chris Lattner wrote:
>
>> When introducing a feature like this, I think it would be useful to
>> survey a range of popular languages (and yes, even perl ;-) to understand
>> what facilities they provide and why (i.e. what problems they are solving)
>> and synthesize a good swift design that can solve the same problems with a
>> hopefully simple approach.
>
>
> Travis Tilley wrote:
>
>> ​Perl and Erlang are unique in that valid code in either language looks
>> essentially like line noise. I'd rather take inspiration from languages
>> like ruby, python, and elixir.​
>
>
> Jokes aside, the ability to choose delimiters for strings and other
> language constructs that surround some value is a huge boon to code
> readability.
>
> For example, RegExp literals in JavaScript:
>
> var regex = /^\/usr\/local\//; // gross
>
> An even simpler example, which applies to many languages: a string literal
> that contains all of your possible string delimiters within it. This is not
> an exotic thing in English.
>
> message = "\"I don't like this,\" she said."; // nope
> message = '"I don\'t like this," she said.';  // still nope
>
> Then, of course, there's your escape character itself:
>
> escapes = "Some escapes: \\n, \\t, \\a"; // sigh
>
> There are many time-tested solutions to these syntactic/cosmetic problems.
>
> * Different delimiters with different interpolation rules (e.g., single
> quotes not honoring any backslash escapes and not doing variable
> interpolation)
>
> * Matched-pair delimiters that don't require anything to be escaped as
> long as the delimiters are absent or matched within the string. (These
> alone solve a huge range of problems.)
>
> * Heredocs for long literals where you get to pick the end token.
>
> * Heredocs modified by delimiters around the end token to control
> interpolation within the long literal.
>
> Which language looks like line noise now?
>
> $messasge = q("I can't believe how nice this is," she said (quietly).);
>
> $regex = qr(^/usr/local/);
>
> $escapes = 'Some escapes: \n, \t, \a';
>
> My take: once you use a language where you pretty much never have to
> backslash-escape a character you can easily type to get it into a string,
> it's really hard to go back.
>
> -John
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/98941097/attachment.html>


More information about the swift-evolution mailing list