5 ms·
Operators are a tradeoff between compactness and ambiguity. They add ambiguity because they are polymorphic, and the order of operations becomes implicit, gover
by jerome-jh 7y ago
Operators are a tradeoff between compactness and ambiguity. They add ambiguity because they are polymorphic, and the order of operations becomes implicit, governed by the operator's priority (for infix or mixed pre/postfix).
Many programming languages abuse the + operator for string concatenation, which is not commutative. This is where my tolerance for operators ends.
- LittlePeter 7y agoI can imagine there exists advanced math where addition is also not commutative, yet they probably use `+` there too. Aren't you throwing the baby away with the bath water?
- cygx 7y agoSuch usage may exist, but it should be rare. In the words of Wikipedia, It is conventional to use the plus sign to only denote commutative operations. For possible alternatives, Perl6 uses ~ for string concatenation (and prefix ~ for stringification). I like it.
- lizmat 7y agoPlease note that Perl 6 has been renamed to Raku (https://raku.org https://raku.org using the #rakulang tag on social media). With regards to using ~ , nothing has changed :-)
- enaaem 7y agoI understand your confusion, but in math the + operator does not have to be commutative either. It often is, but just like in programming, you can define in it anyway you want.
- contravariant 7y agoBy convention the + operator is usually reserved for commutative operations though.
- joppy 7y agoI have never seen + be used for a non-commutative operation in mathematics. I have seen it used for something which is to-be-proven-to-be-commutative in the next page or so. Similarly, I have only ever seen juxtaposition (like “ab”) used for associative operations, I.e satisfying a(bc) = (ab)c.
- yiyus 7y agoMultiplication of octonions is non-associative and usually expressed by simple juxtaposition.
- deleted 7y ago[deleted]
- Silhouette 7y agoThis is a recurring problem when working with floating point values. Intuitions about concepts like associativity or even equality don't necessarily hold, but in many cases you will still get away with it, so it can feel like there's inconsistent or unpredictable behaviour. That can be a big stumbling block for inexperienced programmers working with this type of data at first, and it surely leads to many bugs. 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.
- 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
- jimktrains2 7y agoAre you ok with using + on floating point numbers as it's not truly communative their either.