p = stream || rec.a || rec.b || rec.c;
and get an object which, if written, marshalled the record, and if read, unmarshalled it. The marshalling order was only specified once. Cute, but in retrospect, bad.Python's classic overload problem comes from using "+" for concatenate. For built-in vectors, "+" is concatenate, but for NumPy arrays, it's addition. You can use "+" between a NumPy array and a built-in vector. Does it add, or concatenate?
("|" is supposed to be the concatenate operator; it was added to ASCII to support PL/I. But decades of C have made it the bitwise OR operator to most programmers, so we're stuck. "_" is allowed within symbols, so that's taken. There's "⁀", character tie, but nobody can type it.)
It seems to be best to restrict the math operators to math.
Whether a function for which an operator is overloaded is “cute” or “fundamental to clarity” depends on the context in which it is used.
> There was a fad for this in the early C++ days.
Yeah, like overloading the bitwise shift operators as stream insertion/extraction operators.
> Python's classic overload problem comes from using "+" for concatenate. For built-in vectors, "+" is concatenate
Python doesn't have built-in vectors, it has lists.
> but for NumPy arrays, it's addition.
Not sure I see a problem. For numbers, it's an addition. For simple ordered collections, it's concatenation. For matrix-like collections, it's memberwise addition.
> You can use "+" between a NumPy array and a built-in vector. Does it add, or concatenate?
To the extent that's a problem, it's a problem with implicit coercion more than operator overloading.
The fact that it requires context other than the language spec is where the problem resides. The cognitive overhead of reading code is high enough without having to alias common operators.
The cognitive overhead of reading code is, IME, often lower with context-approprate operator overloading (just as it is with well-chosen vs. poorly-chosen method names.)
There is a reason human languages (and, yes, even the notation of mathematics) develops context-specific dialects, and it is to reduce the cognitive overhead of communication. Code switching with clear contextual boundaries is often far more efficient than using a one-size-fits-all notation that is context-insensitive.
I feel like this makes things like using operator overloading to clean up code incredibly dangerous, especially in popular libraries.
Best: Context-appropriate overload that makes sense to me
Good: Standard language meaning that may not fit cleanly in context, but has the same definition always
Worst: Context-appropriate overload that doesn't make sense to me
So if you aren't sure whether or not your clever idea will actually fall in the third category for some people, go with the second instead of pushing for the first.
Aka, "for every story someone can tell of a magical operator overloading that makes code more readable, I can come up with some dumbass architect who did the most counterintuitive thing you can imagine."
I've worked for a while on a C++ vector/matrix library which made good use of C++'s capabilities to overload operators. It all made sense, in the beginning. But as time wore on and more and more exceptions crept in you'd get very confusing bits of code where in the same fragment '+' would mean one thing, then another, ditto for '*' and endless frustration when no free operator could be overloaded and in the end you'd end up having to call a function anyway.
Maintaining that over the years got less and less pleasant, even if at the start it was probably some of the cleanest code for the limited purpose that it had.
Personally, I avoid operator overloading, just as I try to avoid reader macros and other clever bits. It's not that I don't know how to use them or how they work, it's that I love being able to look at a piece of code without also having to study the environment it operates in in order to know what it does.
KISS. Though, adding two vectors or multiplying a vector and a matrix with just an operator looks very good. Better than:
result_matrix = vec_mat_mul(some_vec, some_matrix);The problem here seems to me to be “having too small a space of valid operators in the host language for the problem domain” more than “operator overloading”, as such. But it's true that the space of available operators is a factor in whether you should use operators over functions for a particular use, because it definitely impacts clarity.
IMO, ideally a language should support a wide range of operators, a d programmers should be very selective in how they use them; additionally, overloaded operators in libraries intended for reuse should usually have equivalent text-named functions/methods, for when the library is used in contexts where using the overloaded operators would be confusing. (Ideally, you'd be able to import selectively so that you don't even bring the operators in scope where you don't want to use them.)
Ok, but in that case you need two systems to access the same code, that only adds to the confusion and really negates all upside from having the overloaded operator in the first place.
That violates DRY in a very ugly way.
Anyway, I think we can agree on one thing: operator overloading is something that one should not do just because it is possible, but only for very good reasons.
Does that work?
I don't think aliasing (or the moral equivalent, where operator vs. method/function can't be an alias from an implementation point of view) violates DRY. Presenting an alternative API for different use cases isn't repetition.
> Anyway, I think we can agree on one thing: operator overloading is something that one should not do just because it is possible, but only for very good reasons.
We agree on that.
Ken Iverson would probably disagree.
Judging from its surviving successors (J particularly), the reason is “keyboards” not “operator overloading”.
Every single time I see printing to an IO stream in C++ I think 'why are they bit-shifting that... oh right'.
Here's C's operator precedence.[1] All 15 levels.
Think of the maintenance programmer who will have to fix this.
[1] http://en.cppreference.com/w/c/language/operator_precedence
People have experimented with relative precedence levels (and I believe Perl6 does this?) where you can say “this is lower-precedence than that” and you have to use parentheses to disambiguate expressions that mix operators unrelated by that partial order.
I didn't know that too. It's pretty cool. Seems like they overload __truediv__ operator: https://github.com/python/cpython/blob/master/Lib/pathlib.py...