Hacker News new | past | comments | ask | show | jobs | submit login

> 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 `..`.




Arguably 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.


In 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.


That’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.


Well now I want a language to overload white space as an operator



But that doesn't help, because * also commutes, and string concatenation still doesn't.


> * also commutes

It does for numbers, but not generally - e.g. for matrices.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: