What are your thoughts on operator overloading?

(on a scale from "I can't live without it" to "I'm literally Drew DeVault")

@codesections Use with great care. I've seen some particularly bad uses; Libreoffice defines '>>' as an assignment to the right in some cases; it confuses the hell out of both readers and all the static analysis tools.
I worked on a (commercial) JIT that originally used overloading to build it's internal structures; we eventually ripped it out as too confusing.

@penguin42 @codesections didn't we also re-invent STL because some of the compilers we used where too old?

@stsquad @codesections Well, fortunately not much of STL (not that I've liked STL's syntax) - but we did avoid it mostly, I think because we wanted it to work on all architectures with touchy/embedded compilers.

@codesections I can live with it for built-in types, but that's where I draw the line. It should be a closed set, defined by the language without any way for application or library programmers to extend it. I'm fine with using '&' to intersect sets in languages where sets are part of the language/standard library, I'm not fine with "this means what I says it means" code. For C++, you have to read the entire program to have any idea what a single line does, and that's only partially hyperbole.

I think it leads to bad code because it introduces more mental load when reading the code. Especially because it is often used in scenarios where it doesn't make sense.

I'd rather have clearly named functions/methods than operators that provide less meaning.

@codesections If it fits nicely in the language (e.g. with typeclasses), sure. If it's ugly hacked on (e.g. Perl), nah.

@codesections Only where it makes sense. In previous years I avoided it more but it has its place.
What I can't stand is when people build a system and add in loads of weird operators to make it more terse. What the result often looks like to an outsider is incomprehensible...

@codesections Make it hard enough to implement so that one who wants to do is has to really think about whether it's worth it.

@codesections honest answer: special syntax should exist exactly for those things for which i am used to reading this special syntax

practical answer: i'd rather have to jump-to-definition once or twice than have my program subtly break because `==' only does the right thing for specific built-in things (looking at you, Java)

@codesections (this only works in a decent development environment where I can ask the system "where does this notation come from?". however, operators are not a necessity to having hard-to-understand code so if my system does not have jump-to-definition i'm screwed anyways)

@codesections "I'm literally Drew deVault" x-D
sorry.... but that is hilarious!

@codesections In general I am not a fan of operator overloading as it requires more context (the type of the arguments) to understand the semantics.
I like custom operators in limited cases, because an operator is an intrinsically meaningless symbols so again it is a matter of how much context is required to understand the code using it.
But for both cases, sometimes the gain in conciseness outweighs the need for more context.

Sign in to participate in the conversation

Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.