[Rd] [WISH / PATCH] possibility to split string literals across multiple lines

luke-tierney at uiowa.edu luke-tierney at uiowa.edu
Thu Jun 15 18:21:41 CEST 2017


On Wed, 14 Jun 2017, Gábor Csárdi wrote:

> 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"""

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.

Another Python idea worth considering is the raw string notation
r"xyx" that does not process escape sequences -- this would make
writing things like regular expressions easier.

Best,

luke

>
> 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
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney at uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu


More information about the R-devel mailing list