[swift-evolution] multi-line string literals.

Leonardo Pessoa me at lmpessoa.com
Fri May 13 11:14:08 CDT 2016


That's my point. You start with a little two-line string and when you see
you have those all over your code. A feature is never bad but a feature
that may be misused creates temptation to go for bad programming practices,
like "I'm using it just this once" and once you're there you never stop.

You can still have multiline string in Swift by concatenating strings as
you already pointed but that discourages you from having too many lines
glued like this. The approach of loading them from a file makes the code
cleaner to read and the string cleaner to maintain and not having the means
to have the lines in your code will push you to do the right thing and load
them from a file from start.


On 13 May 2016 at 01:16, Ricardo Parada <rparada at mac.com> wrote:

> I don't think the intent of multi-line string literals is to replace files
> or templating libraries. I use those all the time and are superior.
>
> I think it would be handy and make it more pleasant to use for simple
> cases.
>
> Who has not run into a little multi-line literal in your code where you
> concatenate the pieces using +, add \n at the end of each line and have to
> escape quotes?
>
>
> On May 12, 2016, at 12:36 PM, Leonardo Pessoa <me at lmpessoa.com> wrote:
>
> I'm not in favour of multiline strings. I believe such strings should be
> stored in plain files and loaded as needed. It makes both the code and the
> string cleaner to read and maintain. I've had experiences with many
> languages that offer that resource and I could see what your code can
> become in terms of maintainability when you have such feature on the
> language.
>
> -1 from me.
>
> - Leonardo
>
> On 12 May 2016 at 01:05, Eduardo Mourey Lopez Ne via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>> Something like this might work
>>
>> //string ends on the first line that doesnt start with a "
>> foo( @"<?xml version="1.0"?>
>>        "<catalog>
>>        "   <book id="bk101" empty="">
>>        "       <author>\(author)</author>
>>        "       <title>XML Developer's Guide</title>
>>        "       <genre>Computer</genre>
>>        "       <price>44.95</price>
>>        "       <publish_date>2000-10-01</publish_date>
>>        "       <description>An in-depth look at creating applications
>> with    XML.</description>
>>        "   </book>
>>        "</catalog>\n
>>     )
>>
>> //additionally using a +" could be used to indicate a line break
>> foo( @"<?xml version="1.0"?>
>>       +"<catalog>
>>       +"   <book id="bk101" empty="">
>>       +"       <author>\(author)</author>
>>       +"       <title>XML Developer's Guide</title>
>>       +"       <genre>Computer</genre>
>>       +"       <price>44.95</price>
>>       +"       <publish_date>2000-10-01</publish_date>
>>       +"       <description>An in-depth look at creating applications
>> with    XML.</description>
>>       +"   </book>
>>       +"</catalog>\n
>>     )
>>
>> On May 11, 2016, at 9:48 PM, Ricardo Parada via swift-evolution <
>> swift-evolution at swift.org> wrote:
>>
>>
>>
>> On May 11, 2016, at 2:34 PM, Vladimir.S <svabox at gmail.com> wrote:
>>
>> For example:
>>
>>
>> letsourceCode =@“NSString *firstName = @“John”;
>>                 "NSString *lastName = @“Doe”;
>>                 “NSString *fullName = [NSString stringWithFormat: @“%@
>> %@“, firstName, lastName];"@
>>
>> The one that would be a bit of a problem is the closing delimiter,
>>
>>
>> Yes.. this is why I asked about `"@` - closing delimiter
>> so.. what is the solution in your case ?
>>
>>
>> Hi Vladimir,
>>
>> I don't really have a solution. Perhaps escaping the closing delimiter
>> like this \"@
>>
>> It is not pretty but I can't think of anything else. I imagine the other
>> alternatives, i.e.   the triple quote `"""` and the quote plus underscore
>> `"_ `  have the same problem.
>>
>> If we make the continuation quote required then we don't need a closing
>> delimiter. That would solve the problem. But I've seen several people say
>> they don't like the continuation quote because they want to be able to
>> paste text and not have to worry much about formatting it afterwards.
>> _______________________________________________
>> 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/20160513/cb7ff188/attachment.html>


More information about the swift-evolution mailing list