From: patrick-may@... (Patrick May) Date: 2002-09-13T08:41:33+09:00 Subject: Re: not grasping the method overloading/multi-dispatch thing Philipp Meier wrote in message news:<20020912131150.GA25625@o-matic.net>... > class Foo > def meth(Integer i, Integer j) > i.to s + j.to s > end > > def meth(Object o1, Object o2) > o1+o2 > end > end what is wrong with something like: class Foo def meth( i, j ) i.to_s + j.to_s end end > Which is reasonably shorted and easier to understand. I see a need for > this as I find myself often manually dispatching to methods by the type > of an argument. Is this a good example of the types of dispatches you often do? Dispatching on type smells like there is a need for some polymorphic method. For example: def description( build_target ) case build_target.class when Compile "compile libraries" when InstallDependencies "install dependencies" else "unknown target" end end (I doubt that code in the wild would be this obvious) To me, the point of OO programming is to have one conditional that picks the type. If you find conditionals on type downstream from your factories, then I think it's a sign of methods that should be on the classes, i.e. Compile.description, InstallDependencies.description, etc. Another point -- it is possible to dispatch on type in Ruby, just not easy. Proposed multimethods wouldn't allow you write new functionality, but it would make it easier to have downstream conditionals on types. In my opinion, I don't think this is a good idea. This kind of code is a smell to me, and I'd rather keep it real smelly to encourage me to clean it up. Throwing some syntax deoderant (sp) would just make it harder for me to see code begging me to midwife some new methods :-) ~ Patrick