operator overloading, when used judiciously, makes for code that is cleaner and easier to read

@typhlosion Hmm! I think I agree, but that word "judiciously" is really important. I'd be interested to hear more detail on your thoughts about good and bad uses of operator overloading, and I think it makes a difference which language you're thinking about. e.g., since Haskell allows inventing any operator you want by stringing together arbitrary symbols, it feels quite different than the C++ habit of using every available operator just to make code more terse―but not always better!

@jamey i'm talking specifically about things like overloading existing infix operators rather than inventing new ones

my personal thoughts are that it only really makes sense if it's intuitive what the operator means in context

the canonical example for me is overloading + on vectors (the mathematical sense) to implement vector addition, and maybe overloading * on vectors for scalar and possibly matrix multiplication. in those cases it's clear what a + b means, so the code is nicer to read

Follow

@jamey so if i want an affine transform aff(x) = Ax+b, i can just have the code say

A*x + b

instead of

A.mul(x).add(b)

or something more verbose and imo less clear

@typhlosion Yes, I strongly agree with you that mathematical uses of operator overloading are Good and Right and should be Encouraged. 😁

I think there are other uses of infix operators that are also good, but the lines are much more fuzzy to me.

And then there's C++ << and >> to read or print data on streams, which I think is Very Bad. 😅

Sign in to participate in the conversation
Awoo Space

Awoo.space is a Mastodon instance where members can rely on a team of moderators to help resolve conflict, and limits federation with other instances using a specific access list to minimize abuse.

While mature content is allowed here, we strongly believe in being able to choose to engage with content on your own terms, so please make sure to put mature and potentially sensitive content behind the CW feature with enough description that people know what it's about.

Before signing up, please read our community guidelines. While it's a very broad swath of topics it covers, please do your best! We believe that as long as you're putting forth genuine effort to limit harm you might cause – even if you haven't read the document – you'll be okay!