From: "Adam P. Jenkins" Date: 2005-07-21T01:20:55+09:00 Subject: Re: Ruby/Rails as a starter language? Devin Mullins wrote: > Adam P. Jenkins wrote: > >> Object polymorph(Object snufkin) { >> Method m = snufkin.getClass().getMethod("foo", null); >> return m.invoke(snufkin, null); >> } >> >> You'll need to add a throws clause to the above method, but otherwise >> that's all there is to it in Java. Ah, you're right, I'd missed that he was calling a class method. In Java you can't add methods to the class object itself as you can in Ruby. Instead it has static methods. So to make polymorph handle static methods, we could behave differently depending on whether the parameter is a Class method: Object polymorph(Object snufkin) throws Exception { if (snufkin instanceof Class) { Method m = snufkin.getMethod("foo", null); return m.invoke(null, null); } else { Method m = snufkin.getClass().getMethod("foo", null); return m.invoke(snufkin, null); } } Anyway, this is clearly not the programming style that Java is optimized for. If your formula for language goodness is "dynamically typed" = good, "statically typed" = bad, then we can keep showing more examples of how doing duck-typing style programming is much better supported in Ruby to show that Ruby is better. Of course I could just reverse the formula, and keep showing examples of all the things a Java compiler can enforce at compile time to show Java is better. The bottom line is that Java has a sufficiently powerful reflection API that it's entirely possible to do duck-typing style programming when you need to, just as it's possible to check types up front in Ruby when you need to, using things like Ara's parseargs library. Adam