4 ms·
I will die on the hill that string concatenation should have its own operator, and overloading + for the operation is a mistake. Languages that get it right: S
by LexiMax 1y ago
I will die on the hill that string concatenation should have its own operator, and overloading + for the operation is a mistake.
Languages that get it right: SQL, Lua, ML, Perl, PHP, Visual Basic.
- zevets 1y agoAlternatively, any implementation of operator+ should have a notional identity element, an inverse element and be commutative.
- AlotOfReading 1y agoC++ would be a very different language if you couldn't use floats: (NaN + 0.0) != 0.0 + NaN Inf + -Inf != Inf I suspect the algebraists would also be pissed if you took away their overloads for hypercomplex numbers and other exotic objects.
- o11c 1y agoI think it's fine when the language has sufficiently strict types for string concatenation. Unfortunately, many languages allow `string + int`, which is quite problematic. Java is to blame for some of this. And C++ is even worse since literals are `const char[]` which decays to pointer. Languages okay by my standard but not yours include: Python, Ruby.
- paulddraper 1y agoThis is so sad obvious it’s painful. Arithmetic addition and sequence concatenation are very very different. —— Scala got this right as well (except strings, Java holdover) Concatenation is ++
- Animats 1y agoPython managed to totally confuse this. "+" for built-in arrays is concatenation. "+" for NumPy arrays is elementwise addition. Some functions accept both types. That can end badly.
- 112233 1y agoTangential, but Lua is the most write-only language I have had pleasure working with. The implementation and language design are 12 out of 10, top class. But once you need to read someone else's code, and they use overloads liberally to implement MCP and OODB and stuff, all in one codebase, and you have no idea if "." will index table, launch Voyager, or dump core, because everything is dispatched at runtime, it's panic followed by ennui.
- butterisgood 1y agoPerl was one for me that I always had a little trouble reading again later. I guess forth as well... hmmm
- Animats 1y ago> string concatenation should have its own operator, It does: | That character was put in ASCII specifically for concatenation in PL/1. Then came C.
- renox 1y agoD (as always) is clever: the operator is ~ So no confusion between addition and concatenation and you can keep | for or.
- Defletter 1y agoQuestion, does that work with other types? Say you have two u16 values, can you concatenate them together with ~ into a u32 without any shifting?
- nicwilson 1y agoIt works with arrays (both fixed size, and dynamically sized) and arrays; between arrays and elements; but not between two scalar types that don't overload opBinary!"~", so no it won't work between two `ushorts` to produce a `uint`
- renox 1y agoNo, it doesn't. But I'm not sure that this matter, a sufficiently "smart" compiler understand that this is the same thing.
- account42 1y agoThose languages need a dedicated operator because they are loosely typed which would make it ambiguous like + in JavaScript. But C++ doesn't have that problem. Sure, a separate operator would have been cleaner (but | is already used for bitwise or) but I have never seen any bug that resulted from it and have never felt it to be an issue when writing code myself.
- _flux 1y agoThough then you can have code like "hello" + "world" that doesn't compile and "hello" + 10 that will do something completely different. In some situations you could actually end up with that by gradual modification of the original code.. Granted this is probably a novice-level problem.
- dgb23 1y agoPHP overloads operators in other ways though.
- CyberDildonics 1y agoBut why, where does it become a problem?