[Rd] Improving string concatenation

Gökçen Eraslan gokcen.eraslan at gmail.com
Wed Jun 24 10:34:13 CEST 2015


On 2015-06-17 20:24, Joshua Bradley wrote:
>> How would this new '+' deal with factors, as paste does or as the current
> '+'
>> does?  Would number+string and string+number cause errors (as in current
>> '+' in R and python) or coerce both to strings (as in current R:paste and
> in perl's '+').
>
>
> I had posted this sample code previously to demonstrate how string
> concatenation could be implemented
>
> "+" = function(x,y) {
>      if(is.character(x) & is.character(y)) {
>          return(paste0(x , y))
>      } else {
>          .Primitive("+")(x,y)
>      }}
>

%+% might have been another option, possibly a more backward-compatible 
one. "paste0 - %+%" pair also resembles "outer - %o%" and "match - %in%" 
pairs.

My 2 cents.

PS: I don't agree that the subject is rather incomplete or just not true.

>
> so it would only happen if both objects were characters, otherwise you
> should expect the same behavior as before with all other classes. This
> would be backwards compatible as well since string+string was never
> supported before and therefore no one would have previously working code
> that could break.
>
> Josh Bradley

Goekcen.



More information about the R-devel mailing list