4 ms·
These "mathy" objects are about the only unambiguous use case for operator overloading. Yes. Most other uses for operator overloading are really chains of func
by Animats 6y ago
These "mathy" objects are about the only unambiguous use case for operator overloading.
Yes. Most other uses for operator overloading are really chains of functions. There's better syntax for that.
I once wrote some overloads for C++ so that you could write
result = fileobject | item | item | item | item;
and then apply either "read" or "write" to "result". This allowed marshalling without writing the item list more than once. Bad idea.
Python is notorious for problems that stem from combining operator overloading and implicit conversions. "+" is arithmetic for some types and concatenation for others.
[1,2,3] + [4,5,6]
is
[1,2,3,4,5,6]
which you might not expect. If you add arrays from numpy, those add arithmetically. You can add a numpy array to a built-in array. Trouble comes when you pass a numpy array to a function that expects a built-in array, and the wrong semantics of "+" are applied.
Rust has operator overloading without implicit conversions. That avoids such ambiguities, at the cost of requiring many explicit conversions.
- samatman 6y agoConflating concatenation and addition is just a terrible mistake, this is one illustration among many of that. In Lua, `+` is always addition, and can be overloaded with an `__add` metamethod, while `..` is concatenation, overloaded with a `__concat` metamethod. `..` is also right associative, which means that `a .. b .. c .. d`, a normal enough string-building pattern, can be optimized into a single allocation for the new string, without having to create `a ..b` and then `ab .. c` and so on. So I don't see this as a problem of operator overloading, I see it as a problem of a missing operator. Implicit conversion makes that problem worse, but `"12" .. 13` in Lua will give you "1213", as you would expect, and `"2" + 3` gives you 5, again, as you would expect.
- Animats 6y agoWhat to use for concatenation is a problem. PL/1 used "|", which was originally drawn with a break in the middle. But C took that over as "or", and that's now the accepted standard. ".." is accepted as a range operator now. Moving deeper into Unicode is probably not the answer. Although, if a language needed a concatenate symbol, ⊞ (Squared Plus) might be a good choice. It's not used for much else.
- lifthrasiir 6y agoAs a binary operator `~` seems pretty unambiguous.
- samatman 6y agoYes, my vote for languages which are running out of glyphs would be `~~` actually. Rather than using context to differentiate bitwise NOT from concatenation, just rule that ~~ is always concatenation. What you lose is just "NOT NOT", which (without this operator) is legal but pointless.
- nicoburns 6y agoPerhaps ++, and just use += 1 for incrementing. Rust already doesn't have an increment operator, and my JavaScript linter forbids it.
- Someone 6y agoIf it didn’t cause problems (ones I can think of in seconds: it complicates parsing, mathematicians will think it signals multiplication, and it makes it hard to insert spaces), using a single space for concatenation would make sense to me. (Aside: C (or the C preprocessor?) is the only piece of software that I know that supports that , but only for string literals)
- fiddlerwoaroof 6y agoConcatenation is just addition, though: addition is the monoid operation on numbers with the identity of 0, concatenation is the monoid operation on lists with identity of []. The problem is almost always not the overloading of + but the implicit type coercion rules
- gowld 6y agoList[Int] has more than one monoid
- fiddlerwoaroof 6y agoBut, when I'm thinking in List[Any], I expect to be working on the Monoid for List[Any], not the Monoid for List[Int]: things like special-casing List[Int] are exactly what make programming languages difficult to learn and use.
- guram11 6y agoand to make things weirder python can concat literal strings together with "space operator"(j/k) >>>'a' 'b' 'c' >>>'abc'
- marvy 6y agothey got rid of that in Python 3, I think
- thaumasiotes 6y ago> Python is notorious for problems that stem from combining operator overloading and implicit conversions. "+" is arithmetic for some types and concatenation for others. `[1,2,3] + [4,5,6]` is `[1,2,3,4,5,6]`, which you might not expect. It seemed obvious to me as soon as I saw it in Perl that the Perl approach ("+" for adding numbers, "." for concatenating strings) is a much better idea than using "+" for both operations. I'm not sure why people keep wanting to use + for concatenation. (My other language pet peeve is using backslash as the escape character for your mini-DSL that gets parsed from strings, when backslash is also the escape character for strings. You want to use separate escape characters for each context; stacking the same one in several contexts is the worst possible approach. C was aware of the problems: it uses `\` as the escape for strings and `%` as the escape for printf commands. But somehow by the time we were writing regular expression libraries, this simple, obvious technology had been lost.)