[Rd] "+" operator on characters revisited

Davor Cubranic cubranic at stat.ubc.ca
Mon Jan 24 20:15:29 CET 2011


On 2011-01-23, at 4:34 AM, Gabor Grothendieck wrote:

> On Sun, Jan 23, 2011 at 6:56 AM, Vitalie S. <spinuvit.list at gmail.com> wrote:
>> Gabor Grothendieck <ggrothendieck at gmail.com> writes:
>> 
>>> Also the gsubfn supports quasi perl style string interpolation that
>>> can sometimes be used to avoid the use of paste in the first place.
>>> Just preface the function in question by fn$ like this:
>>> 
>>> library(gsubfn)
>>> fn$cat("pi = $pi\n")
>> 
>> Thanks for the tip. Not bad indeed.
>> Almost as readable as
>> 
>> cat("pi = " + pi + "\n")
> 
> To me the + can be substantially less readable.  The need to
> repeatedly quote everything makes it just as bad as paste.  Compare
> the following and try to figure out if there is an error in quoting in
> the + and paste solutions.  Trying to distinguish the single and
> double quotes is pretty difficult but simple in the fn$ and sprintf
> solutions.  Even if there were no quotes the constant need to
> interpose quotes makes it hard to read.

That may be a matter of taste, but FWIW it seems that shell-style string interpolation (using the dollar prefix) has going out of style in recent scripting languages. Ruby uses the expression substitution construct ("#{expr}"), while Python has "str.format", both allowing arbitrary expressions.

And most editors have syntax highlighting that distinguishes strings from other program elements. This makes quoting errors pretty obvious.

Davor


More information about the R-devel mailing list