Operator Overloading

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2198 Views - Last Post: 19 May 2012 - 01:12 AM

Poll: Operator Overloading (17 member(s) have cast votes)

Should operator overloading be a 'standard feature' in languages today?

  1. Yes (13 votes [76.47%])

    Percentage of vote: 76.47%

  2. No (4 votes [23.53%])

    Percentage of vote: 23.53%

Do you implement operator overloading in the languages that support it?

  1. Every chance I get (0 votes [0.00%])

    Percentage of vote: 0.00%

  2. When it makes sense (16 votes [94.12%])

    Percentage of vote: 94.12%

  3. Never (1 votes [5.88%])

    Percentage of vote: 5.88%

Vote Guests cannot vote

#1 Vermiculus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 314
  • Joined: 26-February 09

Operator Overloading

Posted 11 May 2012 - 09:03 PM

I've been reading a lot of mixed opinions on operator overloading and the debate between -ease of use- vs. -syntactic sugar-. Obviously (at least to my knowledge) there is no runtime advantage to either approach, so I would presume that the matter is that of preference...

What do you guys think? Do you think language support for operator overloading (or your implementation of such) is something that should be avoided? What connotations lie in the phrase 'syntactic sugar,' anyways?

The function call approach (as opposed to op-ol'ing) is certainly more descriptive and explicit, but this can be a disadvantage as easily as it can be an advantage. Are there situations where you would deem it necessary to overload an operator (such as wrapper classes)? Should languages support operator overloading and leave discretion to the architect? I'm interested in what you guys think about this, as I am sure most of you have quite a few years' experience on me and have seem languages evolve over a longer period of time.

Is This A Good Question/Topic? 0
  • +

Replies To: Operator Overloading

#2 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Operator Overloading

Posted 11 May 2012 - 11:24 PM

I wouldn't mind reading these 'mixed opinions' you speak of about it. I would really like to know why someone would say "no" and "never" to your question.
Was This Post Helpful? 0
  • +
  • -

#3 NecroWinter  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 318
  • Joined: 21-October 11

Re: Operator Overloading

Posted 12 May 2012 - 01:48 AM

I think it all comes down to programming style. i think operator overloading can make things more readable, just like defining your own types is.

some people know how to do both and just prefer not to, nothing wrong with that.
Was This Post Helpful? 0
  • +
  • -

#4 Vermiculus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 314
  • Joined: 26-February 09

Re: Operator Overloading

Posted 12 May 2012 - 06:18 AM

Against:


For:

Was This Post Helpful? 0
  • +
  • -

#5 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: Operator Overloading

Posted 14 May 2012 - 09:33 AM

I don't see any valid reasons against operator overloading. Everything I've seen suggested can be said of all functions, not just operators.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • Joined: 29-May 08

Re: Operator Overloading

Posted 14 May 2012 - 09:48 AM

Which is "better" ?
Using the .Method Notation.
var Party = New Party();
Party.Add(Mark);
Party.Add(Laura);
Party.Add(Adam);



or

Using Operator Overloading
var Party  = Mark + Laura;
    Party += Adam;





Using operator overloading to implement settings.

Lights += -Landing /* Negatation (Off) */
Lights += +Porch   /* Positive   (On )*/
Lights += +Kitchen 
Lights += +Lounge
Lights += -Bathroom
Lights += (Bedroom - 7) /* Turn down low */



I'm sure I've seen a RegEx (with a minor tweak) Builder that use Operator Overloading.

This post has been edited by AdamSpeight2008: 14 May 2012 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Operator Overloading

Posted 14 May 2012 - 09:51 AM

I make use of operator overloading all the time in C# and I love it.

For example we find ourselves writing classes that represent mathematical objects, which operator overloading really tends itself to. We can use overloading to out in add methods etc etc.

Coming from the Java world I can say that methods called .add() etc are a massive pain in the ass in terms of readability (and writability). As you have sometimes very long expressions, operator overloading looks like it should do, whereas with methods you end up with massive amounts of nested method calls that are horrible in almost every way.

However I do find that the opportunity to actually use operator overloading is quite restricted. I think a lot of people use them wrongly where methods should be used. For example overloading the OR operator to union database queries. It may work, however it doesn't really make sense to. If I were not making mathematical classes all the time then I doubt I would use them much at all.

This post has been edited by Ryano121: 14 May 2012 - 09:53 AM

Was This Post Helpful? 0
  • +
  • -

#8 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Operator Overloading

Posted 14 May 2012 - 10:12 AM

Operator overloading is just like ternary operator: used correctly is awesome, abused is a hell of a mess.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • Joined: 29-May 08

Re: Operator Overloading

Posted 14 May 2012 - 10:22 AM

Especially a Pain in posterior when you get/assume the ordering/associativity of the operators is correct.
Was This Post Helpful? 0
  • +
  • -

#10 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Operator Overloading

Posted 14 May 2012 - 10:52 AM

View PostVermiculus, on 12 May 2012 - 08:18 AM, said:



Oh Java, the home of ass backwards ideals.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,883
  • Joined: 19-March 11

Re: Operator Overloading

Posted 14 May 2012 - 01:04 PM

Meh. Java is designed to be an enterprise language. The assumption is that your code and your company will be successful, therefore the code will be maintained and extended by people who didn't write it, and who aren't superstar coders.

Everything that people complain about with Java stems from that assumption, and this is no exception. Consistency of syntax is useful if you're maintaining someone else's code base, and requiring method calls to be made explicitly keeps the syntax consistent. The real problems with Java are the places where they've sweetened things up, like autoboxing, which break the programmer's assumptions about what should happen.
Was This Post Helpful? 0
  • +
  • -

#12 Vermiculus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 314
  • Joined: 26-February 09

Re: Operator Overloading

Posted 14 May 2012 - 10:11 PM

View PostRyano121, on 14 May 2012 - 12:51 PM, said:

Coming from the Java world I can say that methods called .add() etc are a massive pain in the ass in terms of readability (and writability). As you have sometimes very long expressions, operator overloading looks like it should do, whereas with methods you end up with massive amounts of nested method calls that are horrible in almost every way.


This consistency is what languages should be all about, imho. Programs are the direct products of thoughts, and if your program (ie syntax) isn't consistent, how then can your thoughts?

I definitely hear you on the ... annoying bit - I got to a point in one of my projects where I simply could not stand it. I have on disk a small little script-thing-that's-technically-not-a script that will expand the macro of operator overloading for me (given precedences) and leave a commented 'overloaded' version above it. I know it's a pain, but infix is just too ambiguous. Without knowing how a certain language/compiler handles precedence, it is impossible to know what will happen. God forbid you needing to debug it.

Much of what we do nowadays is built off of what are essentially text-expanding macros, so I don't see why doing a little bit of the macro thing ourselves is such a sore point.

AdamSpeight2008, while your implementation of op. overloading for settings is clever, it seems like it's pushing on ambiguous as to what it actually does (comments aside). The use of methods is simply more stable and understandable (when formatted nicely, it rather resembles LISP).

On the flip side, BigInteger. It's obvious that languages shouldn't restrain programmers in their jobs, but... I don't know. For the few applications that make consistent sense (mathematical wrappers, such as numbers, matrices, etc.), I don't know if it's worth it to have such dangerously ambiguous code when it is so easy to write an expander.
Was This Post Helpful? 1
  • +
  • -

#13 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: Operator Overloading

Posted 15 May 2012 - 06:42 AM

Should operator overloading be a 'standard feature' in languages today?

This depends on the language. The question doesn't really make sense for assembly, far less for regex!

Operator overloading was deliberately omitted in Java to remove a common source of confusion and bugs. It's the same reason it doesn't have pointers or multiple inheritance. It might be a bit dull and the workarounds are often a bit verbose but at least you are encouraged to structure what you can write nicely.

Operator overloading is entirely appropriate in languages like c++ and Ruby. They are designed to give the programmer a lot of flexibility. There is freedom to write some really great code but also freedom to hang yourself.

If I were designing a language today, I would have to look very carefully at why I was writing the language in the first place. I would have to consider where I want my language to sit on the flexibility/safety spectrum. But I'd also need to take into account what typical applications the language would have. If I was designing something for use in maths or science, I might find the expressiveness of operator overloading attractive. For business solutions, maybe not so much.

Do you implement operator overloading in the languages that support it?

Not really. Most of my programming in the last 10 years has been in Java which does not support operator overloading. When I do program in something else, I usually write a method before I even think of overloading an operator. I am trying to branch out though so this will hopefully change in the near future.
Was This Post Helpful? 2
  • +
  • -

#14 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Operator Overloading

Posted 16 May 2012 - 06:37 AM

I think the question applied to high level turing complete languages so regex(not, at all turing complete) and assembly seem a moot point.

operator overloading allows for a far less verbose syntax than would be possible using explicit names. it's an abstraction, that's what high level languages do for us. if it doesn't complicate the language too much then it's not a bad idea as it's a very useful abstraction.

This post has been edited by ishkabible: 16 May 2012 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#15 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Operator Overloading

Posted 16 May 2012 - 12:30 PM

operator overloading is moving down the right path as far as Domain Specific Language's go. They tend to ease the use of what would otherwise be clumsy syntactic constructs. However, most LISP dialects go one step further and allow you to interact with the language expressions themselves (which makes developing DSL's even easier!).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2