[Rd] Improving string concatenation

Gábor Csárdi csardi.gabor at gmail.com
Wed Jun 17 15:19:42 CEST 2015


On Wed, Jun 17, 2015 at 9:04 AM, Michael Lawrence
<lawrence.michael at gene.com> wrote:
> Just to clarify, primitive (C-level) generics do not support dispatch
> on basic classes (like character). This is for performance (no need to
> consider dispatch on non-objects) and for sanity (in general,
> redefining fundamental behaviors is dangerous). It is of course
> possible to define a "+" method with a signature containing a class
> not in the set of basic classes.

I see, thanks for pointing this out.

Still, I see this as a technicality. The current "+" clearly detects
if it gets a non-numeric argument, because it gives an error message
for it. So in this case it could just check if both sides are
characters, and if that's true, concatenate them. So there is no
performance loss at all.

This is obviously not as clean as a dispatch, but I think it is still
better than requiring people to add classes to their strings,
especially if the strings are literals.

Btw. for some motivation, here is a (surely incomplete) list of
languages with '+' as the string concatenation operator:

ALGOL 68, BASIC, C++, C#, Cobra, Pascal, Object Pascal, Eiffel, Go,
JavaScript, Java, Python, Turing, Ruby, Windows PowerShell,
Objective-C, F#, Scala, Ya.

and there are a lot of others that have a different operator for it:

Haskell, Erlang, Ada, AppleScript, COBOL (for literals only), Curl,
Seed7, VHDL, Visual Basic, Excel, FreeBASIC, Perl, PHP, Maple, Icon,
Standard SQL, PL/I, Rexx, Mathematica, Lua, Smalltalk, OCaml, Standard
ML, F#, rc, Fortran.

Source: https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(strings)

Yes, even Fortran has one, and in C, I can simply write "literal1"
"literal2" and they'll be concatenated. It is only for literals, but
still very useful.

Best,
Gabor



More information about the R-devel mailing list