From: Chad Fowler Date: 2003-09-14T19:19:07+09:00 Subject: Re: Duck Typing On Sun, 14 Sep 2003, Ryan Pavlik wrote: # On Sun, 14 Sep 2003 15:22:37 +0900 # Martin DeMello wrote: # # # > Any yes, it's by no means specific to Ruby, but it is the way the # > language defines a 'type'. # # No, it's how _you the programmer_ define a 'type'. You have a few # options here; "duck typing" is just one of them. It has nothing to do # with what the languages specifies in this case. # It is impossible to use Ruby without using duck typing. As you semi-paraphrase/semi-assert in a previous message: " By the definitions given in this thread, duck typing is taking a given object and pretending it's the type you want, and letting the language give you an error if it doesn't work like you expected." Given Ruby's dynamic nature, even if you have an object of the class you expected at compile-time, you're going to fall into a "not-a-duck" scenario in the case of something like: class String undef_method :chomp end def some_method(a,b) expect(a, String, b, Numeric) # I'm using your "Strong"Typing module. ;) a.chomp #Whoops! "a" is not the duck you're looking for # (but it *is* a String)! ...etc. end Since I happen to be on the topic of your "Strong"Typing module, let's have a look at the FAQ: Q: Yeah, but really, why bother? Why not just let ruby sort out the errors as they occur? A: This is incorrect thinking. Allowing errors to just occur when they happen is naive programming. Consider the following: # Wait N seconds, then open the bridge for M seconds def sendMsg(bridge, n, m) sleep(n) bridge.open sleep(m) bridge.close end Now say 'm' is pased in as a string. Oops! A TypeError is raised.... Bummer. I guess it's only possible to do naive programming with Ruby. I'm sure there are some Frankenstein-like mechanisms that could be strapped onto ruby to get closer to this goal of "sophisticated" programming, but I hope I don't live to see them. Chad (who suffers from this so-called "incorrect" thinking)