[swift-evolution] multi-line string literals

Brent Royal-Gordon brent at architechies.com
Tue Dec 15 23:08:29 CST 2015


> One corner case: how do you write a string literal that starts or ends with “?    How do you express the equivalent of “\”foo”?  A concern is that this could make escape processing “feel” less predictable.

Technically, the rule that there must be an odd number of double-quotes in the delimiter solves this: `“”””foo”””` can only be interpreted as `”foo` delimited by `“””`. But that’s obviously not an ideal answer.

One possibility is that, when you use a multi-character string literal delimiter, you could insert a single whitespace character after the opening delimiter (and any continuations on other lines), and before the terminating delimiter:

	let foo = """ "To be, or not to be;
	          """  That is the question." "”" 
	// => “\”To be, or not to be;\n that is the question.\”"

To keep this from introducing ambiguity about whitespace stripping, the space would be required, and its absence would trigger a syntax error. I’m not totally satisfied with this approach, though.

> I still think we’d want a way to disable escape processing.

I agree, but I think it’s orthogonal to this proposal. Whether we use `’foo'`, or `r”foo”`, or `@“foo”` (following C#), or `@raw “foo”`, that can easily extend to multiline and long-delimiter strings.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list