[swift-evolution] [Review] SE-0168: Multi-Line String Literals

Ricardo Parada rparada at mac.com
Fri Apr 14 18:56:24 CDT 2017


I agree that it is not obvious. 

At one point I argued that the trailing newline on the last line should be stripped. But for this reason and others I am now in the camp that thinks we should leave the trailing newline alone. 

If we don't want to include the trailing newline we can always do this:

let str = """
   Line 1
   Line 2\
   """

This is were it's difficult to get consensus. 

Regards,
Ricardo 


> On Apr 14, 2017, at 5:54 PM, BJ Homer via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I’m not saying that the + operator should automatically add a newline. I’m saying that both strings should contain a trailing newline, such that the visible result is the same.
> 
> By contrast, this would feel really strange:
> 
> let a = """
> This is line one
> This is line two
> 
> """
> 
> let b = """
> This is line three
> This is line four
> """
> 
> (a + b) == """
> This is line one
> This is line two
> This is line three
> This is line four
> """
> 
> On initial intuition, it seems strange that ‘a’ has a blatantly visible blank line at the end which seemingly “disappears” when the strings are concatenated. If I think about it for a bit, I can understand why that would be the case, but I think it’s non-obvious.
> 
> -BJ
> 
>> On Apr 14, 2017, at 3:49 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>> 
>> I disagree. I expect the last result to be from `a + "\n" + b`, for the reasons I outlined earlier.
>> 
>> The concatenation operator + does not introduce implied separators when joining strings. There is no reason to think that it should for multi-line strings specifically.
>>> On Fri, Apr 14, 2017 at 16:35 BJ Homer via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>>> Consider these two examples:
>>>> 
>>>> let string_1 = """foo"""
>>>> 
>>>> 
>>>> let string_2 = """
>>>>     foo
>>>>     """
>>>> What’s the intuitive result you’d expect without taking all the long talk from the list into account?
>>>> 
>>>> Personally, I’d say string_1 == string_2 is true.
>>>> 
>>> 
>>> I think it’s reasonable to expect them to be different, actually. I might call these “single-line” and “multi-line” mode strings. The single-line mode is primarily useful for being able to include unescaped double-quotes in the string. If you’re in multi-line mode, though, it’s reasonable to be thinking about things in terms of “lines”, and having a trailing newline there seems reasonable. For example, I think it’s reasonable to expect this:
>>> 
>>> let a = """
>>> This is line one
>>> This is line two"
>>> """
>>> 
>>> let b = """
>>> This is line three
>>> This is line four
>>> """
>>> 
>>> (a + b) == """
>>> This is line one
>>> This is line two
>>> This is line three
>>> This is line four
>>> """
>>> 
>>> That seems like a reasonable model to work with multi-line strings.
>>> 
>>> -BJ
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170414/c7e1e511/attachment.html>


More information about the swift-evolution mailing list