[Rd] "+" for character method...

Martin Maechler maechler at stat.math.ethz.ch
Fri Aug 25 18:31:27 CEST 2006


This thread remains me of an old recurring (last May!) theme 
which maybe fits well to Friday late afternoon...

There have been propositions to make  "+"  work  in S (and R)
like in some other languages,
namely for character (vectors),
 
     a + b :=  paste(a,b, sep="")

IIRC, when this theme came up last, the one argument against it
was the penalty of method dispatch that we were not willing to
pay for something as fundamentally speed-important as "+" --
which is a .Primitive in R exactly for that reason of efficiency.

But then, we actually do dispatch for "+" -- internally in C
code via DispatchGroup()  --- but only if we need, so not when usual
numeric/complex arguments are used.

I think - but may be wrong - it should be possible to also check
very fast for two "character" arguments and in that case do a fast
version of  paste(a, b,  sep="").

When this last came up (in May), Brian said that about the fact
that you could not just simply define "+.character"

>> I would think that the intention was also to positively discourage messing 
>> with the basics of R, as if you were able to do this erroneous uses would 
>> likely not get caught.
   ( https://stat.ethz.ch/pipermail/r-help/2006-May/104751.html )

and subsequently (https://stat.ethz.ch/pipermail/r-help/2006-May/104754.html)
gave an example for this

>> 2 + x, for example, where x is not numeric.

I wonder however, if we do this in C, and basically only go into
the paste-branch when both arguments are characters,
if we wouldn't get to a nice useful solution without a noticable
performance penalty.

This would also solve my other slight related uneasyness :
Many times in the past, when using paste(..., sep='')
in function definitions I had wanted this (empty sep) to be the
default and to have an easier, more readable way to achieve the
same.

But then these all are just musings at the end of the week...

Martin Maechler, ETH Zurich



More information about the R-devel mailing list