[swift-evolution] multi-line string literals

Jason Dusek jason.dusek at gmail.com
Fri Dec 11 18:43:24 CST 2015


On Fri, 11 Dec 2015 at 15:48 Kametrixom Tikara via swift-evolution
swift-evolution at swift.org <http://mailto:swift-evolution@swift.org> wrote:

I think it can just work like this:
>
> struct Test {
>     func hi() {
>         let json =
>         ```             // Indentation to the left of the back ticks gets
> erased and nothing more
>         {
>             "Hello": 3
>         }
>         ```
>
>
>         let str =
>         ```
>         "This is a string"
>     ```     // Compiler error: Back ticks not aligned
>
>
>         let hs =
>         ```
>     main = interact id      // Compiler error: too little indentation
>         ```
>     }
> }
>
>
By just not supporting more complex indentation erasure, we can avoid
> potential confusion.
>
I support the triple backticks — they seem more distinct and avoid the
double/single quote controversy (and ambiguity).

One use case for a slightly richer approach to indentation erasure — one
that I encounter every day — is inline SQL. If it’s 10 lines or more, it
should probably find expression as a VIEW or stored procedure; but queries
like this are not unusual:

func findMentions(account: String) -> Array<String> {
    let db = databaseFactory().findDatabase()
    let q = `​``SELECT array_agg(mentioner.id)
                 FROM account
                 JOIN mention ON (account.id = mentioned)
                 JOIN account AS mentioner ON (mentioner.id = mentioner)
                WHERE account.handle = :1
                ORDER BY timestamp DESC
                LIMIT 1`​``

    return db.query(q).parameterize(account).run()
}

Moving everything below q seems infelicitous, at best. However, I do see
the appeal of basing the rule on the position of the backticks.

Best Regards,

Jason Dusek
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151212/448d28d2/attachment.html>


More information about the swift-evolution mailing list