Radford Neal
2017-Jun-16 18:04 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
> On Wed, 14 Jun 2017, G?bor Cs?rdi wrote: > > > I like the idea of string literals, but the C/C++ way clearly does not > > work. The Python/Julia way might, i.e.: > > > > """this is a > > multi-line > > lineral""" > > luke-tierney at uiowa.edu:> This does look like a promising option; some more careful checking > would be needed to make sure there aren't cases where currently > working code would be broken.I don't see how this proposal solves any problem of interest. String literals can already be as long as you like. The problem is that they will get wrapped around in an editor (or not all be visible), destroying the nice formatting of your program. With the proposed extension, you can write long string literals with short lines only if they were long only because they consisted of multiple lines. Getting a string literal that's 79 characters long with no newlines (a perfectly good error message, for example) to fit in your 80-character-wide editing window would still be impossible. Furthermore, these Python-style literals have to have their second and later lines start at the left edge, destroying the indentation of your program (supposing you actually wanted to use one). In contrast, C-style concatenation (by the parser) of consecutive string literals works just fine for what you'd want to do in a program. The only thing they wouldn't do that the Python-style literals would do is allow you to put big blocks of literal text in your program, without having to put quotes around each line. But shouldn't such text really be stored in a separate file that gets read, rather than in the program source? Radford Neal
Gábor Csárdi
2017-Jun-16 18:14 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
On Fri, Jun 16, 2017 at 7:04 PM, Radford Neal <radford at cs.toronto.edu> wrote:>> On Wed, 14 Jun 2017, G?bor Cs?rdi wrote: >> >> > I like the idea of string literals, but the C/C++ way clearly does not >> > work. The Python/Julia way might, i.e.: >> > >> > """this is a >> > multi-line >> > lineral""" >> >> luke-tierney at uiowa.edu: > >> This does look like a promising option; some more careful checking >> would be needed to make sure there aren't cases where currently >> working code would be broken. > > I don't see how this proposal solves any problem of interest. > > String literals can already be as long as you like. The problem is > that they will get wrapped around in an editor (or not all be > visible), destroying the nice formatting of your program.>From the Python docs:String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it?s possible to prevent this by adding a \ at the end of the line. [...] Gabor
Hadley Wickham
2017-Jun-16 19:10 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
On Fri, Jun 16, 2017 at 1:14 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> wrote:> On Fri, Jun 16, 2017 at 7:04 PM, Radford Neal <radford at cs.toronto.edu> wrote: >>> On Wed, 14 Jun 2017, G?bor Cs?rdi wrote: >>> >>> > I like the idea of string literals, but the C/C++ way clearly does not >>> > work. The Python/Julia way might, i.e.: >>> > >>> > """this is a >>> > multi-line >>> > lineral""" >>> >>> luke-tierney at uiowa.edu: >> >>> This does look like a promising option; some more careful checking >>> would be needed to make sure there aren't cases where currently >>> working code would be broken. >> >> I don't see how this proposal solves any problem of interest. >> >> String literals can already be as long as you like. The problem is >> that they will get wrapped around in an editor (or not all be >> visible), destroying the nice formatting of your program. > > From the Python docs: > > String literals can span multiple lines. One way is using > triple-quotes: """...""" or '''...'''. End of lines are automatically > included in the string, but it?s possible to prevent this by adding a > \ at the end of the line.And additionally, in Julia triple quoted strings: Trailing whitespace is left unaltered. They can contain " symbols without escaping. Triple-quoted strings are also dedented to the level of the least-indented line. This is useful for defining strings within code that is indented. For example: Hadley -- http://hadley.nz
Duncan Murdoch
2017-Jun-16 19:17 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
On 16/06/2017 2:04 PM, Radford Neal wrote:>> On Wed, 14 Jun 2017, G?bor Cs?rdi wrote: >> >>> I like the idea of string literals, but the C/C++ way clearly does not >>> work. The Python/Julia way might, i.e.: >>> >>> """this is a >>> multi-line >>> lineral""" >> >> luke-tierney at uiowa.edu: > >> This does look like a promising option; some more careful checking >> would be needed to make sure there aren't cases where currently >> working code would be broken. > > I don't see how this proposal solves any problem of interest. > > String literals can already be as long as you like. The problem is > that they will get wrapped around in an editor (or not all be > visible), destroying the nice formatting of your program. > > With the proposed extension, you can write long string literals with > short lines only if they were long only because they consisted of > multiple lines. Getting a string literal that's 79 characters long > with no newlines (a perfectly good error message, for example) to fit > in your 80-character-wide editing window would still be impossible. > > Furthermore, these Python-style literals have to have their second > and later lines start at the left edge, destroying the indentation > of your program (supposing you actually wanted to use one). > > In contrast, C-style concatenation (by the parser) of consecutive > string literals works just fine for what you'd want to do in a > program. The only thing they wouldn't do that the Python-style > literals would do is allow you to put big blocks of literal text in > your program, without having to put quotes around each line. But > shouldn't such text really be stored in a separate file that gets > read, rather than in the program source?I agree with most of this, but I still don't see the need for a syntax change. That's a lot of work just to avoid typing "paste0" and some commas in paste0("this is the first part", "this is the second part") If the rather insignificant amount of time it takes to execute this function call really matters (and I'm not convinced of that), then shouldn't it be solved by the compiler applying constant folding to paste0()? (Some syntax like r"xyz" to make it easier to type strings containing backslashes and quotes would actually be useful, but that's a different issue.) Duncan Murdoch
peter dalgaard
2017-Jun-16 20:17 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
> On 16 Jun 2017, at 21:17 , Duncan Murdoch <murdoch.duncan at gmail.com> wrote: > > paste0("this is the first part", > "this is the second part") > > If the rather insignificant amount of time it takes to execute this function call really matters (and I'm not convinced of that), then shouldn't it be solved by the compiler applying constant folding to paste0()?And, of course, if it is equivalent to a literal, it can be precomputed. There is no point in having it in the middle of a tight loop. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Reasonably Related Threads
- [WISH / PATCH] possibility to split string literals across multiple lines
- [WISH / PATCH] possibility to split string literals across multiple lines
- [WISH / PATCH] possibility to split string literals across multiple lines
- [WISH / PATCH] possibility to split string literals across multiple lines
- [WISH / PATCH] possibility to split string literals across multiple lines