IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New double dispatch example
Double dispatch is used when you would really like to dispatch on two different objects at the same time.

Example: Suppose you were manipulating geometric shapes (Bryce will love this example), and you wanted to determine the intersection of two arbitrary shapes.
def process(shape1, shape2)\n  shape1.intersect(shape2)   # This is the first dispatch\nend
Now, the way an intersection is caculated is different depending on your shapes. For example, finding the intersection of two rectangles is done differently from finding the intersection of two circles, which in turn is different from a rectangle and a circle.

Now, if we know the type of one of the shapes, the algorithm is easy to write for any other shape. For example:
  class Rectangle\n    def intersect_rectangle(rect)\n      puts "Intersecting a Rectangle and a Rectangle"\n    end\n\n    def intersect_circle(circle)\n      puts "Intersecting a Rectangle and a Circle"\n    end\n  end\n\n  class Circle\n    def intersect_rectangle(rect)\n      puts "Intersecting a Circle and a Rectangle"\n    end\n\n    def intersect_circle(circle)\n      puts "Intersecting a Circle and a Circle"\n    end\n  end\n
Notice that each type knows how to intersect itself with both a circle and a rectangle.

Suppose I execute the code: rect.intersect(shape)? We dispatch to the retangle's intersection (this is the first dispatch). We don't know the type of shape, but we do know the type of self (which is a rectangle). Well, the problem is simple if we know one of the type (which we do). We just dispatch again. Here is the intersection code for rectangle:
  class Rectangle\n    def intersect(shape)\n      shape.intersect_rectangle(self)  # this is the second dispatch\n    end\n  end
The code for circle is a mirror image.
  class Circle\n    def intersect(shape)\n      shape.intersect_circle(self)  # this is also a second dispatch\n    end\n  end
Finally, we put it together:
  c1 = Circle.new\n  c2 = Circle.new\n  r1 = Rectangle.new\n  r2 = Rectangle.new\n\n  process(c1,c2)\n  process(r1, r2)\n  process(r1, c1)\n  process(c1, r1)
And the output is
  Intersecting a Circle and a Circle\n  Intersecting a Rectangle and a Rectangle\n  Intersecting a Circle and a Rectangle\n  Intersecting a Rectangle and a Circle
In short, double dispatch is a way of doing type discovery.

Advantages: it is fast and bounded. It takes just two polymorphic calls to resolve the types. A dynamic cast in Java or C++ is generally slower and depends on the shape of the inheritance tree.

Disadvantages: the number of dispatching methods grows with the square of the number of types to distinquish.
--
-- Jim Weirich jweirich@one.net [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New ahh, thanks
===

Implicitly condoning stupidity since 2001.
     Smalltalk GLORP => Ruby Criteria - (JimWeirich) - (6)
         Looks pretty cool - (tuberculosis) - (5)
             OT: double dispatch? - (drewk) - (3)
                 double dispatch example - (JimWeirich) - (1)
                     ahh, thanks -NT - (drewk)
                 Yep - its all through the numerics - (tuberculosis)
             Re: Looks pretty cool - (JimWeirich)

Where's web cam sex show Barbie?
51 ms