This is all runtime processing. I think the indentation stripping is better 
done at compile time. 

You should be able to select the multi-line string constants in your array of 
stories example and indent them to look good in your code. Then just use the 
closing delimiter to tell the compiler how much indentation to strip. 

Here is Brent's excellent proposal in case you have not seen it:

https://github.com/johnno1962a/swift-evolution/blob/master/proposals/0168-multi-line-string-literals.md


> On Apr 14, 2017, at 11:12 PM, Muse M <james.le...@gmail.com> wrote:
> 
> This would be prefer to follow Scala multiline string which look
> 
> 1) //Default 
> var code = """header
> paragraph1"""
> 
> Output:
> >header
> >paragraph1
> 
> 2)
> var code = """
> ...header...
> ...paragraph1...
> """.replace("...")
> 
> Output:
> >header
> >paragraph1
> 
> 3)
> var code = """
>          |..header...
>          |..paragraph1...
> """.stripLeadingSpace()
> 
> Output:
> >header...
> >paragraph1...
> 
> 4)
> var code = """
> 
> import Foundation
> print("Hello World!")
> 
> """.code()
> 
> Output:
> >
> >import Foundation
> >print("Hello World!")
> >
> 
> 5)
> //To remove all new lines
> var code = """
> import Foundation
> 
> import Cocoa
> 
> ....print("Hello World!")
> """.compactLine()
> 
> Output:
> >import Foundation
> >import Cocoa
> >....print("Hello World!")
> 
> 
> 
> On Multiline String collection
> 
> 6) If we are importing many sources from 3rd parties API, we won't know if 
> their code is properly indent, some use tab and some use 2 or 4-space. This 
> would look better on Swift Playground app.
> 
> var stories = {
> """
> Chapter1
> 第1章
> 
> Introduction to A
> """,
> """
> Chapter2
> 第2章
> 
> Introduction to B
> """,
> """
> //Code Example
>     import Foundation
> """
> }
> 
> We can loop or apply to certain collection
> stories(3).code()
> 
> 
> 
>> On Saturday, April 15, 2017, Ricardo Parada via swift-evolution 
>> <swift-evolution@swift.org> wrote:
>> 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@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...@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@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@swift.org
>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution@swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to