[swift-evolution] multi-line string literals
Jordan Rose
jordan_rose at apple.com
Mon Dec 14 15:58:25 CST 2015
The main issue here is recovery. If you see a newline in a string, is it more likely that the literal is intended to go on for multiple lines, or that the user hasn't finished typing the current line? If you assume the former, your "in a string" state will toggle with the next double-quote you see, and you'll get a cascade of errors down the whole file.
(The general principle: when adding new syntax, it's important to consider not just complete, correct code, but also the possible error and intermediate states that the compiler and SourceKit have to deal with.)
Jordan
> On Dec 11, 2015, at 23:02, Alex Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>
> What is the reason that normal strings "..." don't support newlines? It's not traditionally seen in C-inspired languages, but it's hardly unusual outside that sphere[*]. Ruby and HTML are probably the most common examples, but apparently OCaml and Lisps also allow it?
>
> Then we can simply use
>
> 1. "..." strings as the verbatim literal, preserving indentation and leading newlines as written.
>
> 2. """...""" strings as the non-verbatim literal, reducing indentation to that of the least indented line, and removing the leading newline if it exists.
>
> [*] As seen earlier http://rigaux.org/language-study/syntax-across-languages.html#StrngMltLine <http://rigaux.org/language-study/syntax-across-languages.html#StrngMltLine>
>
> On Sat, Dec 12, 2015 at 6:25 AM, Travis Tilley via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> That's another great example that I hadn't even thought of.
>
>
> - Travis Tilley
>
>
> On Fri, Dec 11, 2015 at 7:43 PM, Jason Dusek <jason.dusek at gmail.com <mailto:jason.dusek at gmail.com>> wrote:
> 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 <http://mentioner.id/>)
> FROM account
> JOIN mention ON (account.id <http://account.id/> = mentioned)
> JOIN account AS mentioner ON (mentioner.id <http://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
>
>
>
> _______________________________________________
> 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>
>
>
> _______________________________________________
> 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/20151214/b3e0954c/attachment.html>
More information about the swift-evolution
mailing list