Andreas Kersting
2017-Jun-14 10:58 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
Hi, I would really like to have a way to split long string literals across multiple lines in R. Currently, if a string literal spans multiple lines, there is no way to inhibit the introduction of newline characters: > "aaa + bbb" [1] "aaa\nbbb" If a line ends with a backslash, it is just ignored: > "aaa\ + bbb" [1] "aaa\nbbb" We could use this fact to implement string splitting in a fairly backward-compatible way, since currently such trailing backslashes should hardly be used as they do not have any effect. The attached patch makes the parser ignore a newline character directly following a backslash: > "aaa\ + bbb" [1] "aaabbb" I personally would also prefer if leading blanks (spaces and tabs) in the second line are ignored to allow for proper indentation: > "aaa \ + bbb" [1] "aaa bbb" > "aaa\ + \ bbb" [1] "aaa bbb" This is also implemented by this patch. An alternative approach could be to have something like ("aaa " "bbb") or ("aaa ", "bbb") be interpreted as "aaa bbb". I don't know the ins and outs of the parser of R (hence: please very carefully review the attached patch), but I guess this would be more work to implement!? What do you think? Is there anybody else who is missing this feature in the first place? Regards, Andreas -------------- next part -------------- A non-text attachment was scrubbed... Name: patch.diff Type: text/x-patch Size: 2598 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20170614/7986e543/attachment.bin>
Duncan Murdoch
2017-Jun-14 11:12 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
On 14/06/2017 5:58 AM, Andreas Kersting wrote:> Hi, > > I would really like to have a way to split long string literals across > multiple lines in R.I don't understand why you require the string to be a literal. Why not construct the long string in an expression like paste0("aaa", "bbb") ? Surely the execution time of the paste0 call is negligible. Duncan Murdoch> > Currently, if a string literal spans multiple lines, there is no way to > inhibit the introduction of newline characters: > > > "aaa > + bbb" > [1] "aaa\nbbb" > > > If a line ends with a backslash, it is just ignored: > > > "aaa\ > + bbb" > [1] "aaa\nbbb" > > > We could use this fact to implement string splitting in a fairly > backward-compatible way, since currently such trailing backslashes > should hardly be used as they do not have any effect. The attached patch > makes the parser ignore a newline character directly following a backslash: > > > "aaa\ > + bbb" > [1] "aaabbb" > > > I personally would also prefer if leading blanks (spaces and tabs) in > the second line are ignored to allow for proper indentation: > > > "aaa \ > + bbb" > [1] "aaa bbb" > > > "aaa\ > + \ bbb" > [1] "aaa bbb" > > This is also implemented by this patch. > > > An alternative approach could be to have something like > > ("aaa " > "bbb") > > or > > ("aaa ", > "bbb") > > be interpreted as "aaa bbb". > > I don't know the ins and outs of the parser of R (hence: please very > carefully review the attached patch), but I guess this would be more > work to implement!? > > > What do you think? Is there anybody else who is missing this feature in > the first place? > > Regards, > Andreas > > > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Gábor Csárdi
2017-Jun-14 11:17 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
On Wed, Jun 14, 2017 at 12:12 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> On 14/06/2017 5:58 AM, Andreas Kersting wrote: >> >> Hi, >> >> I would really like to have a way to split long string literals across >> multiple lines in R.You can also look at the glue package, it supports continuation and a lot more: glue(" A formatted string \\ can also be on a \\ single line ") #> A formatted string can also be on a single line Gabor [...]
Andreas Kersting
2017-Jun-14 11:45 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
On Wed, 14 Jun 2017 06:12:09 -0500, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> On 14/06/2017 5:58 AM, Andreas Kersting wrote: > > Hi, > > > > I would really like to have a way to split long string literals across > > multiple lines in R. > > I don't understand why you require the string to be a literal. Why not > construct the long string in an expression like > > paste0("aaa", > "bbb") > > ? Surely the execution time of the paste0 call is negligible. > > Duncan MurdochActually "execution time" is precisely one of the reasons why I would like to see this feature as - depending on the context (e.g. in a tight loop) - the execution time of paste0 (or probably also glue, thanks Gabor) is not necessarily insignificant. The other reason is style: I think it is cleaner if we can construct such a long string literal without the need for a function call. Andreas> > > > Currently, if a string literal spans multiple lines, there is no way to > > inhibit the introduction of newline characters: > > > > > "aaa > > + bbb" > > [1] "aaa\nbbb" > > > > > > If a line ends with a backslash, it is just ignored: > > > > > "aaa\ > > + bbb" > > [1] "aaa\nbbb" > > > > > > We could use this fact to implement string splitting in a fairly > > backward-compatible way, since currently such trailing backslashes > > should hardly be used as they do not have any effect. The attached patch > > makes the parser ignore a newline character directly following a backslash: > > > > > "aaa\ > > + bbb" > > [1] "aaabbb" > > > > > > I personally would also prefer if leading blanks (spaces and tabs) in > > the second line are ignored to allow for proper indentation: > > > > > "aaa \ > > + bbb" > > [1] "aaa bbb" > > > > > "aaa\ > > + \ bbb" > > [1] "aaa bbb" > > > > This is also implemented by this patch. > > > > > > An alternative approach could be to have something like > > > > ("aaa " > > "bbb") > > > > or > > > > ("aaa ", > > "bbb") > > > > be interpreted as "aaa bbb". > > > > I don't know the ins and outs of the parser of R (hence: please very > > carefully review the attached patch), but I guess this would be more > > work to implement!? > > > > > > What do you think? Is there anybody else who is missing this feature in > > the first place? > > > > Regards, > > Andreas > > > > > > > > ______________________________________________ > > R-devel at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > >
Simon Urbanek
2017-Jun-14 13:48 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
As I recall this has been discussed at least a few times (unfortunately I'm traveling so can't check the references), but the justification was never satisfactory. Personally, I wouldn't mind string continuation supported since it makes for more readable code (I had one of my packages raise a NOTE in examples because there is no way in R to split a long hash into multiple lines), but I would be strongly against random removal of whitespaces as it's counter-intuitive, misleading and makes it impossible to continue spaces on the next line. None of the languages that I can think of with multiline strings do that as that's way too dangerous. Cheers, Simon> On Jun 14, 2017, at 6:58 AM, Andreas Kersting <r-devel at akersting.de> wrote: > > Hi, > > I would really like to have a way to split long string literals across multiple lines in R. > > Currently, if a string literal spans multiple lines, there is no way to inhibit the introduction of newline characters: > > > "aaa > + bbb" > [1] "aaa\nbbb" > > > If a line ends with a backslash, it is just ignored: > > > "aaa\ > + bbb" > [1] "aaa\nbbb" > > > We could use this fact to implement string splitting in a fairly backward-compatible way, since currently such trailing backslashes should hardly be used as they do not have any effect. The attached patch makes the parser ignore a newline character directly following a backslash: > > > "aaa\ > + bbb" > [1] "aaabbb" > > > I personally would also prefer if leading blanks (spaces and tabs) in the second line are ignored to allow for proper indentation: > > > "aaa \ > + bbb" > [1] "aaa bbb" > > > "aaa\ > + \ bbb" > [1] "aaa bbb" > > This is also implemented by this patch. > > > An alternative approach could be to have something like > > ("aaa " > "bbb") > > or > > ("aaa ", > "bbb") > > be interpreted as "aaa bbb". > > I don't know the ins and outs of the parser of R (hence: please very carefully review the attached patch), but I guess this would be more work to implement!? > > > What do you think? Is there anybody else who is missing this feature in the first place? > > Regards, > Andreas > <patch.diff>______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Hadley Wickham
2017-Jun-14 14:51 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
On Wed, Jun 14, 2017 at 8:48 AM, Simon Urbanek <simon.urbanek at r-project.org> wrote:> As I recall this has been discussed at least a few times (unfortunately I'm traveling so can't check the references), but the justification was never satisfactory. > > Personally, I wouldn't mind string continuation supported since it makes for more readable code (I had one of my packages raise a NOTE in examples because there is no way in R to split a long hash into multiple lines), but I would be strongly against random removal of whitespaces as it's counter-intuitive, misleading and makes it impossible to continue spaces on the next line. None of the languages that I can think of with multiline strings do that as that's way too dangerous.Julia does, but uses triple quotes: https://docs.julialang.org/en/stable/manual/strings/#triple-quoted-string-literals Hadley -- http://hadley.nz
Serguei Sokol
2017-Jun-14 15:06 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
Le 14/06/2017 ? 12:58, Andreas Kersting a ?crit :> Hi, > > I would really like to have a way to split long string literals across multiple lines in R. > > ... > An alternative approach could be to have something like > > ("aaa " > "bbb")This is C-style and if the core-team decides to implement it, it could be useful and intuitive.
William Dunlap
2017-Jun-14 15:12 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
If you are changing the parser (which is a major change) you might consider treating strings in the C/C++ way: char *s = "A" "B"; means the same as char *s = "AB"; I am not a big fan of that syntax but it is widely used. A backslash at the end of the line leads to errors when you accidently put a space after the backslash and the editor doesn't flag it. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Jun 14, 2017 at 3:58 AM, Andreas Kersting <r-devel at akersting.de> wrote:> Hi, > > I would really like to have a way to split long string literals across > multiple lines in R. > > Currently, if a string literal spans multiple lines, there is no way to > inhibit the introduction of newline characters: > > > "aaa > + bbb" > [1] "aaa\nbbb" > > > If a line ends with a backslash, it is just ignored: > > > "aaa\ > + bbb" > [1] "aaa\nbbb" > > > We could use this fact to implement string splitting in a fairly > backward-compatible way, since currently such trailing backslashes should > hardly be used as they do not have any effect. The attached patch makes the > parser ignore a newline character directly following a backslash: > > > "aaa\ > + bbb" > [1] "aaabbb" > > > I personally would also prefer if leading blanks (spaces and tabs) in the > second line are ignored to allow for proper indentation: > > > "aaa \ > + bbb" > [1] "aaa bbb" > > > "aaa\ > + \ bbb" > [1] "aaa bbb" > > This is also implemented by this patch. > > > An alternative approach could be to have something like > > ("aaa " > "bbb") > > or > > ("aaa ", > "bbb") > > be interpreted as "aaa bbb". > > I don't know the ins and outs of the parser of R (hence: please very > carefully review the attached patch), but I guess this would be more work > to implement!? > > > What do you think? Is there anybody else who is missing this feature in > the first place? > > Regards, > Andreas > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]
Gábor Csárdi
2017-Jun-14 15:18 UTC
[Rd] [WISH / PATCH] possibility to split string literals across multiple lines
I don't think it is reasonable to change the parser this way. This is currently valid R code: a <- "foo" "bar" and with the new syntax, it is also valid, but with a different meaning. Or you can even consider a <- "foo" bar %>% func() %>% print() etc. 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""" Gabor On Wed, Jun 14, 2017 at 4:12 PM, William Dunlap via R-devel <r-devel at r-project.org> wrote:> If you are changing the parser (which is a major change) you > might consider treating strings in the C/C++ way: > char *s = "A" > "B"; > means the same as > char *s = "AB"; > > I am not a big fan of that syntax but it is widely used. > > A backslash at the end of the line leads to errors when you accidently > put a space after the backslash and the editor doesn't flag it. > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Wed, Jun 14, 2017 at 3:58 AM, Andreas Kersting <r-devel at akersting.de> > wrote: > >> Hi, >> >> I would really like to have a way to split long string literals across >> multiple lines in R. >> >> Currently, if a string literal spans multiple lines, there is no way to >> inhibit the introduction of newline characters: >> >> > "aaa >> + bbb" >> [1] "aaa\nbbb" >> >> >> If a line ends with a backslash, it is just ignored: >> >> > "aaa\ >> + bbb" >> [1] "aaa\nbbb" >> >> >> We could use this fact to implement string splitting in a fairly >> backward-compatible way, since currently such trailing backslashes should >> hardly be used as they do not have any effect. The attached patch makes the >> parser ignore a newline character directly following a backslash: >> >> > "aaa\ >> + bbb" >> [1] "aaabbb" >> >> >> I personally would also prefer if leading blanks (spaces and tabs) in the >> second line are ignored to allow for proper indentation: >> >> > "aaa \ >> + bbb" >> [1] "aaa bbb" >> >> > "aaa\ >> + \ bbb" >> [1] "aaa bbb" >> >> This is also implemented by this patch. >> >> >> An alternative approach could be to have something like >> >> ("aaa " >> "bbb") >> >> or >> >> ("aaa ", >> "bbb") >> >> be interpreted as "aaa bbb". >> >> I don't know the ins and outs of the parser of R (hence: please very >> carefully review the attached patch), but I guess this would be more work >> to implement!? >> >> >> What do you think? Is there anybody else who is missing this feature in >> the first place? >> >> Regards, >> Andreas >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
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