Hi, One of the features, I have heard that Ruby has, is unbounded polymorphism. How exactly is it different from polymorphism in languages like Java and C#? Thanks
on 2007-02-05 01:50
on 2007-02-05 03:00
On Feb 4, 7:48 pm, mrun...@gmail.com wrote: > Hi, > One of the features, I have heard that Ruby has, is unbounded > polymorphism. > How exactly is it different from polymorphism in languages like Java > and C#? Think of a bound as an upper limit with respect to the class hierarchy. If a method lists a parameter as taking a Shape, and Circle and Rectangle are subclasses of Shape, then instances of Shape, Circle, and Rectangle can be passed into that method (unless Shape is an abstract class in which case there are no instances of it). Instances that are not at or below Shape are excluded from being passed in as that parameter. The same thing is true with Java generics. If you declare a variable as being of type List<Shape>, then only instances of Shape, Circle, and Rectangle can be added to that list. All this is possible due to the static type of Java. Ruby, on the other hand, does not have static typing. Types are not associated with variable or parameter declarations. So you can assign any piece of data to any variable and you can pass in any type of data into any parameter. Now that may sound a little wild initially. However, if you try to call an unsupported method on that variable or parameter, then you'll get a runtime error and an opportunity to debug.* But if that data supports all the methods that are called on it, it doesn't matter what type it is. You often hear this referred to as "Duck Typing", because if it walks like a duck and talks like a duck then for all intents and purposes, it's a duck (i.e., if it can *do* what you ask it to do, then you don't care what it "really" is). Hope that helps, Eric *: There's a caveat to this in that you can create a hook to "capture" these calls to missing methods and respond to them programatically. This is done by declaring a method named "method_missing" that will, as you might guess, get called when the method is missing.
on 2007-02-05 03:58
That really helped me understand it clearly. Thanks :)