From: "Benjamin J. Tilly" Date: 2001-05-09T00:46:01+09:00 Subject: [ruby-talk:14869] Re: Polymorphic methods >===== Original Message From Dave Thomas ===== >"Benjamin J. Tilly" writes: > >> >> m = Month.new(12) >> >> m = Month.new('December') >> >> m = Month.new(anotherMonth) >> > >> >I'd like to know how to do that in ruby too, but my impression is >> >that you just have to test the type. > >> But there is no way to remove the fact that if the type is to matter >> then it must be tested, and logic that exists must be written. > >Ben: > >I think that might be a little absolute :) Wasn't it you who posted >the double-dispatch example a couple of days ago? We could code the >above without a condition in sight (apart from validation). In the paragraph before the snippet you show, I said explicit or implicit. Hiding the test in a method lookup is what I meant by implicit. But the lookup is still there. It needs to be performed by the interpreter, understood by the programmer. It does not come totally free. At the risk of raising some controversy, consider the mixins that are being worked on to make it easy to create new datatypes that masquerade as native ones. If you hide the condition in a new method lookup, it is unlikely that the mixin will know about your custom methods. Therefore the new datatypes will not work as drop-in replacement in code that needs the custom methods. Suppose that you notice this and wanted to fix it. Well you need to know how the dispatch happened and move logic to the appropriate place so that it is picked up by all data types. Now this is going to be easier than with an explicit dispatch on the named types. Ruby has indeed bought you something with the reorganization of your logic. But it still costs something. Cheers, Ben