Many programming languages abuse the + operator for string concatenation, which is not commutative. This is where my tolerance for operators ends.
Many programming languages abuse the + operator for string concatenation, which is not commutative. This is where my tolerance for operators ends.
This is why Julia uses `*` for string concatenation, which they claim is “more natural” than `+`. Not sure I agree - I’d rather see a completely separate operator for concatenation, such as Haskell’s `++` or Lua’s `..`.
Some languages seem to allow it for string literals though.
julia> x = 10
10
julia> 2x
20
julia> 2 x
ERROR: syntax: extra token "x" after end of expression
But you have to be careful not to use white space either.Not the most useful of features, but it does explain why * is the concat operator.
It does for numbers, but not generally - e.g. for matrices.
With experience, programmers adjust their intuitive expectations and guarding against these kinds of bugs becomes second nature, and to some extent that is probably the best we can do because of the nature of the beast. However, it does illustrate the cost of violating intuitive expectations about how familiar operators and functions will behave.
For possible alternatives, Perl6 uses ~ for string concatenation (and prefix ~ for stringification). I like it.