The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Operator Overloading Stupidity

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Joey Gibson

Posts: 71
Nickname: joeygibson
Registered: Jun, 2003

Joey Gibson is an opinionated software architect who blogs about technology, politics, music, etc.
Operator Overloading Stupidity Posted: Feb 17, 2004 3:56 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Joey Gibson.
Original Post: Operator Overloading Stupidity
Feed Title: Joey Gibson's Blog
Feed URL:
Feed Description: Thoughts, musings, ramblings and rants on Java, Ruby, Python, obscure languages, politics and other exciting topics.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Joey Gibson
Latest Posts From Joey Gibson's Blog

I'm back in .NET land after living, pretty much exclusively, for the last two years in Java-and-Ruby land and while I generally like C#, I ran across something today that is really stupid. It involves operator overloading. While I actually like operator overloading and think it can be a powerful thing, the implementation of one facet of it in C# is just wrong. The problem is that they force you to override certain operators in pairs instead of defining them in terms of each other or in terms of a single method. In other words, if I define == to check equality then I also have to define != to check inequality! Why in the world should I have to do that since inequality is the opposite of equality and can be defined in terms of the equality operator? The same holds true for < & > and <= & >=. What you end up with is something like this:
 1  public class Foo
 2  {
 3      private int val;
 5      public Foo(int val)
 6      {
 7          this.val = val;
 8      }
10      public static bool operator==(Foo lhs, Foo rhs)
11      {
12          return lhs.val == rhs.val;
13      }
15      public static bool 
operator!=(Foo lhs, Foo rhs)
16      {
17          return ! (lhs == rhs);
18      }
19  }
where the != operator just negates a call to ==. You would repeat this process for the other two pairs if you wanted to provide them. (And yes, I know if you provide your own == then you have to also provide a GetHashCode method.) There is no reason that the developer should have to do anything more than provide a comparison operator and then let the system define the other methods in terms of that comparison...

And that's exactly what Ruby does. If you include the module called "Comparable" in your class, and provide a comparison method called <=> (called the "spaceship method" because that's what it looks like) then you get ==, !=, <, <=, > and >= for free! That's right, by defining one method you get six defined in terms of it automatically. Now that's how it should be done! Which results in code like this:
 1  class Foo
 2      include Comparable  # Here's where the majic starts
 4      attr_accessor :val
 6      def initialize(val)
 7          self.val = val
 8      end
10      # comparison method; all others defined in terms of this
11      def <=>(other)
12          val <=> other.val
13      end
14  end
Obviously these are both trivial classes but I think you get the point. There is no reason why you should have to define more than one method to get all the normal comparisons that you would expect in a language.

Read: Operator Overloading Stupidity

Topic: Registered for RubyConf 2003 Previous Topic   Next Topic Topic: Ruby in Panther

Sponsored Links


Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use