3 ms·
> Many programming languages abuse the + operator for string concatenation, which is not commutative. This is why Julia uses `*` for string concatenation, whic
by pansa2 7y ago
> Many programming languages abuse the + operator for string concatenation, which is not commutative.
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 `..`.
- contravariant 7y agoArguably the simplest notation for string concatenation would be just concatenation (which would be the notation for multiplication, hence why Julia's choice could be argued to eb the most natural). Unfortunately programming languages can't quite deal with the ambiguity of having `a (x + y)` and `f(x)` refer to different operations. Some languages seem to allow it for string literals though.
- leethargo 7y agoIn Julia, you can actually omit the `*` for multiplication, if it involves literals, i.e. 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.
- yunruse 7y agoThat’s quite clever, actually. This way you could chain a complex string together with parentheses without needing a lot of pluses. Not the most useful of features, but it does explain why * is the concat operator.
- Avshalom 7y agoWell now I want a language to overload white space as an operator
- gpderetta 7y agoC++2000 has you covered: http://www.stroustrup.com/whitespace98.pdf http://www.stroustrup.com/whitespace98.pdf
- AnimalMuppet 7y agoBut that doesn't help, because * also commutes, and string concatenation still doesn't.
- pansa2 7y ago> * also commutes It does for numbers, but not generally - e.g. for matrices.
- deleted 7y ago[deleted]