Why have a special operator for string concatenation?

Having .. as a separate operator reduces the opportunities for confusion.

Overloading '+' to mean string concatenation is a long tradition. But concatenation is not addition, and it is useful to keep the concepts separate, In Lua, strings can convert into numbers when appropriate (e.g 10+'20') and numbers can convert into strings (e.g 10 .. 'hello'). Having separate operators means that there is no confusion, as famously happens in JavaScript.

When overloading operators, this distinction is also useful. For instance, a List class can be created where .. again means concatenation, that is, joining different lists together to make a longer list. We could also define + for lists of numbers, and then it could mean element-wise addition, that is v+u is a list containing the sums of the elements of v and u, that is, they are treated as vectors.



Back