From: Mark Hubbart Date: 2005-01-15T08:22:38+09:00 Subject: Re: Duck Typing and automated Conversions Hi, I think I probably was somewhat unclear in my other post. I wasn't arguing for or against your idea (for the most part), I was going off it and thinking up a related idea... I guess it was actually pretty off topic. On Sat, 15 Jan 2005 05:06:13 +0900, Robert Klemme wrote: > > "Mark Hubbart" schrieb im Newsbeitrag > news:de63abca0501141100154206a8@mail.gmail.com... > > > I do this to make sure I'm allowing for maximum flexibility, or when > > working with other's code, to find out what the actual requirements > > are :) > > Well, then you'll learn that your instance must implement to_int. Same as > if someone had coded that directly in the method. I don't see this as an > argument against my suggested shortcut syntax, because that mainly affects > the *writer* of a method - not the *caller*. My only argument against the shortcut syntax is that I can't see how often it could be used, except to simulate static typing. I suspect that adding a feature like this would encourage the use of static typing in methods. In your example method, you called #to_int on an object that you were just going to call #times on. This doesn't guarantee that you will get an integer, and makes it difficult for others to use duck typing in code that interfaces with yours. In fact, if they need the functionality, then the have to resort to some hack like this: obj = ObjectThatImplementsTimes.new wrapper = Object.new class << wrapper;self;end.__send__(:define_method, :to_int){obj} A very ugly hack, just to re-allow duck typing. > > There are a couple of problems still: First, an object might still be > > able to respond to a message, using method_missing, but #respond_to? > > won't show it. > > You introduced the #respond_to? - my suggestion was to simply invoke it and > see what happens (duck typing). I was describing something else; I was describing a possible problem with *my* implementation of a related idea. OT, I know :/ > > Second, I'm not sure about the syntax. It would work > > (it currently give a parse error), but I think something better and > > more self-explanatory could be found. > > Sure. I'm very open here. I think the selection of "." as separator was > not good. We can change that any time. But the real issue for me is, does > this make sense independend of syntax? I still think that this is, for the most part, an implementation of static typing. It may just be me, but I very rarely use to_str, to_int, etc, in my code. The only time I do is when I absolutely have to have those particular classes. Which is so uncommon for me, I can't think of the last time I used it. But as I said, I may very well be in the minority here. cheers, Mark