[swift-evolution] [Review] SE-0168: Multi-Line String Literals
Gwendal Roué
gwendal.roue at gmail.com
Thu Apr 13 08:25:37 CDT 2017
Doesn't the principle of least surprise applies here?
In most other languages, the final \n is implicit in multi-line strings and heredocs: I think """↵foo↵foo↵" should be equal to "foo\nfoo\n", because this is what most developers will assume.
Yes it will not play well with the standard print() function. Swift is discovering that being as handy as a scripting language is not an easy task. But the problem lies in all the printing / stdout / stderr API, not in the string literals.
Gwendal Roué
> Le 13 avr. 2017 à 15:17, Adrian Zubarev via swift-evolution <swift-evolution at swift.org> a écrit :
>
> As already mentioned I revised my opinion about the last content line, because the model I summarized mimics perfectly how you’d write text paragraphs.
>
> I’d say most of the time string concatenation is used to combine strings on the same line. A string literal however should only represent the content between the delimiters. Including an implicit new line for the last content line might seem consistent to other content lines from s0 to s(n - 1) (I refer to my gist here), but it should be consistent to the standard string literal instead.
>
> print("""
> foo
> foo
> """)
> That will only print
>
> foo
> foo
> As expected (I don’t take the new line from print into account). That is the natural an consistent way.
>
> If you’d need a new line at the end you’ll have natural ways of doing so, or you can fallback to the explicit \n character.
>
> print("""
> foo
> foo\n
> """)
>
> // Equivalent to the print above this comment, because the backslash
> // in the last line only serves precision, but has no effect on the
> // implicit new line character, because the last line doesn't add any.
> print("""
> foo
> foo\n\
> """)
>
> // Natural text way
> print("""
> foo
> foo
>
> """)
>
>
>
> --
> Adrian Zubarev
> Sent with Airmail
>
> Am 13. April 2017 um 14:35:59, Ricardo Parada (rparada at mac.com <mailto:rparada at mac.com>) schrieb:
>
>> Trailing newline
>>
>> We need consensus on whether the trailing newline on the last line should be included. You are proposing it should not be included which will add an empty line to Brent's xml concatenation example. The example looks better when the trailing newline is included.
>>
>> In the more rare case where the newline is not desired on the last line then you can just include the backslash at the end. See Brent's revised proposal: https://github.com/johnno1962a/swift-evolution/blob/master/proposals/0168-multi-line-string-literals.md <https://github.com/johnno1962a/swift-evolution/blob/master/proposals/0168-multi-line-string-literals.md>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170413/06223cb9/attachment.html>
More information about the swift-evolution
mailing list