[swift-evolution] multi-line string literals

Drew Crawford drew at sealedabstract.com
Fri Dec 11 01:06:56 CST 2015


> The reason I raise the question is that some languages have multiple quote styles (Perl 5 has something like 3 or 4 different string literal styles IIRC?) with different policies.  One reason for this is to disable processing of escapes: if you’re using string literals to enter something that uses \ or “ frequently, it can be irritating and ugly to have a lot of \\'s <smb://'s> <smb://'s <smb://'s>>.  In some dialects of inline assembly in C, for example, this can lead to very ugly code.
> 
> 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.

While I think surveying the literature for inspiration is good, it is even better to look at motivating usecases.  “multi-line string literals” is a feature everybody can get behind, but I wonder if there is agreement on the motivation.

For me, the motivation is that I am pasting the string from some external source.  Suppose I’m writing JSON parsing code, and I paste `let json = <JSON>" at the top of the playground to test with.  

> I would want multi-line string literals to behave as much like normal
> string literals as possible for consistency. To optimize for developer
> happiness, to steal the ruby saying, unexpected behavior should be kept to
> a minimum. If \n works in a normal string literal, it should work in a
> multi-line string literal... even if you could just hit enter instead.


Well… the principle of least surprise depends entirely on what surprises you.  If you start with a one-line string and then say “nevermind, two lines” then having the escape behavior change out from under you is surprising.  On the other hand, if you are pasting long text from an external source, opting *into* escaping behavior is surprising.

The triple-quote proposal at least avoids the issue of accidentally prematurely terminating a pasted string in a lot of common cases.  (JSON strings, for example.)  However that is almost worse in some sense, as the other escape sequences are much more subtle than a compile error, and likely to slip past developer notice.

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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/51625c07/attachment.html>


More information about the swift-evolution mailing list