7 ms·
Overloading operators for cute purposes is usually a misfeature. There was a fad for this in the early C++ days. I once wrote a marshalling library which overl
by Animats 9y ago
Overloading operators for cute purposes is usually a misfeature. There was a fad for this in the early C++ days. I once wrote a marshalling library which overloaded "||", so that you could write
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.
- nradov 9y agoAnother approach taken in some programming languages is to dispense with operators entirely and just use functions, with fairly loose restrictions on function names. For example, in principle there's no reason a function couldn't be named "+" or "|". Obviously that can lead to readability problems.
- Jach 9y agoAnd then in some of those languages they let you create macros so in the cases where the normal function syntax looks ugly you can make it readable with no cost... But with macros comes the same sort of potential abuse as other operator overloading, so it's a tradeoff.
- dragonwriter 9y ago> Overloading operators for cute purposes is usually a misfeature 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.
- jacquesm 9y ago> Whether a function for which an operator is overloaded is “cute” or “fundamental to clarity” depends on the context in which it is used. 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.
- dragonwriter 9y ago> 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.
- schoen 9y agoAnother interesting example is in PyParsing, which can overload + and | and a few other things to produce BNF-style grammar parsers inside of Python code.
- Diederich 9y agoI'm pleased to see this perspective represented; it's the main reason I still really love the Perl programming language. The idea that context is useful and can actually help with clarity is a core concept.
- ethbro 9y agoThe problem is that a reasonable context response is different from person to person and culture to culture. 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."
- coldtea 9y ago>It seems to be best to restrict the math operators to math. Ken Iverson would probably disagree.
- Animats 9y agoI've coded in APL. There's a reason it died out. Although it was the first language with "do this on all that stuff" operators.
- dragonwriter 9y ago> I've coded in APL. There's a reason it died out. Judging from its surviving successors (J particularly), the reason is “keyboards” not “operator overloading”.
- marcosdumay 9y agoAbout the parenthesis, Haskell got "<>". It's ugly as hell, but it's ugly in a way that is easy to get used to.
- chrisseaton 9y ago> Overloading operators for cute purposes is usually a misfeature Every single time I see printing to an IO stream in C++ I think 'why are they bit-shifting that... oh right'.
- evincarofautumn 9y agoYup, overloading existing operators for new meanings is smelly. It’s better if the language allows creating new operators, so you’re not stepping on someone else’s semantics. For example, Haskell has “</>” in the “filepath” package as an alias of “combine”. Custom operators can certainly be abused, but most packages that define operators only do so for good reason, and crucially they’re still searchable with Hoogle.
- Animats 9y agoYou can even set the precedence of your new operator in Haskell, and confuse everybody. Maybe beyond +, -, *, and /, you should have to use parentheses. 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 http://en.cppreference.com/w/c/language/operator_precedence
- evincarofautumn 9y agoEh, I’ve gotten somewhat confusing type errors from mixing precedence levels in Haskell, but no bugs that I can recall. I’d like it if the levels were named instead of numbered, though—I have a hunch that the majority of operators fall into a family like “additive”, “multiplicative”, &c. 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.
- VyseofArcadia 9y agoYou probably despise a lot of popular Haskell libraries.
- daurnimator 9y agoLua uses '..' as the concatenation operator.
- Animats 9y agoNow that's a good idea.
- yellowapple 9y agoOn the opposite end of the spectrum, you have the likes of Perl where you get all sorts of specialized operators to avoid such ambiguities, at the expense of having to learn a whole bunch of operators.