3

Some languages have a . operator for string concatenation. The oldest language I could find that supports it is Perl. Was Perl the first to use it? Why was it chosen?

4
  • 4
    Ask Larry. He had some... unique... ideas for why things are the way they are in Perl. – user40980 May 28 '15 at 20:48
  • 2
    Wow. So why is this a terrible question, but programmers.stackexchange.com/questions/43329/… is ok. Are folks down-voting this because they feel it is off-topic? – eebbesen May 28 '15 at 21:05
  • 3
    @eebbesen because the other question was asked four years ago and is a general term used throughout the industry (and before). On the other hand, this is asked why a particular coder (known for a number of idiosyncrasies) made a particular design choice. – user40980 May 28 '15 at 21:11
  • 1
    @MichaelT - This wasn't meant to be Perl-specific. I just couldn't find a reference to the dot being used for string concatenation in any languages older than Perl. I've edited the question to (hopefully) reflect that. – Tyler Holien May 28 '15 at 21:14
4

If you want to stick with a single non-alphanumeric non-whitespace ASCII character for operators, there really aren't that many. I can only see a couple of alternative choices: !, ~, #, ,, and $. Of those, only . and , can be reached without on a US keyboard, # is the comment character. Comma makes kind of sense, but it is already used for a different purpose in C and C-like languages, with which a lot of Perl programmers would also be familiar, and so has the same meaning in Perl.

This leaves you only with the dot. Note that a middle dot (·) is used in maths to denote function composition (and Haskell uses the ASCII dot as an approximation for that), which can be kinda-sorta related to concatenation.

There is, in fact, no standard operator symbol for concatenation in maths, some suggestions are the double plus (Haskell uses ++ for concatenation) or the frown .

Some languages use + for concatenation, which is a terrible choice, because concatenation lacks several of the properties that we normally associate with an addition-like operation. Most importantly, concatenation is noncommutative.

9
  • 4
    + is an ideal choice for concatenation, because no one expects joining two strings together to be an arithmetical operation in the first place. Saying "+ is bad because it doesn't have the properties of arithmetical addition" is only a valid claim if + in this context not having the properties of arithmetical addition would violate POLS and confuse users, but since no one expects it to, it is not a bad thing. It's far more intuitively correct, producing a string that is the result of adding the contents of one string to another, than . is, which intuitively... does what? – Mason Wheeler May 28 '15 at 21:31
  • 3
    @MasonWheeler from the perl world (which this seems to come from), + is an arithmetic only operator. In the perl world, the operator is only used in one context (string or number) to avoid ambiguities with data types ("4" + 3 is what? - in perl, it is 7 because + is a math operator). Overloading an operator for different contexts is not an option there. – user40980 May 28 '15 at 21:39
  • 4
    @MichaelT: The problem with that is weak typing and has nothing to do with operators. + for concatenation is a fine choice. Nobody expects literally every symbol in a programming language to have completely and only it's strict mathematical meaning (for most anyway I'm sure there's a few where you have to program in Unicode and strictly mathematical notation). – DeadMG May 28 '15 at 21:44
  • 2
    @DeadMG It is a consequence of weak typing that necessitates a differentiation of operator contexts (or you end up with some other fun). Working from the assumption that perl was the first to use it, the proposed alternative of using + isn't an option because of these design decisions. – user40980 May 28 '15 at 21:46
  • 3
    @MasonWheeler The problem with + is that it's also overloaded for chars such that the "a" + 'b' -> "ab" but 'a' + 'b' -> 195, which can bite you in the ass when concatenating if you ever end up with two adjacent chars. – Doval May 28 '15 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.