From: Ryan Pavlik Date: 2003-09-14T04:22:11+09:00 Subject: Re: Duck Typing On Sat, 13 Sep 2003 17:13:17 +0900 Hal Fulton wrote: > > Is one slower, less clean or ... ? I am just curious... > > Don't misunderstand. David is saying that this is *some* people's > impression of what duck typing means. > > Note the second part: "--when in fact it's really Ruby that's doing > the duck typing, and you're just deciding how to react to it." > > It's not a coding style or technique; it's an attribute of the > language. I disagree, with some proof by counterexample. The assertion is this: Ruby does not have an attribute of the language that is "duck typing", it is merely a style of coding. 1) 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. (If you disagree with this definition, you can provide an alternate one, but arguing over definitions is useless.) 2) We can accomplish this because the language lets us specify a call to a method without checking the type at "compile-time" (parse-time, in ruby's case): x.a # This call is not validated until it is executed 3) We can further accomplish this because the language does not require we specify types to a methodcall, thus allowing us to pass any object. 4) Statements 2-3 are the only functional provisions necessary for accomplishing the practice in statement 1. 5) Many non-Ruby dynamic languages provide the functionality in statements 2-3. Examples include JavaScript, Common Lisp (CLOS), Scheme, Python, Perl, and PHP. 6) These languages make no special provisions for duck typing, but it can be practiced, because the functional requirements of 2-3 are met. 7) In any of the given languages, and Ruby, one can choose to check types or not check types. Therefore: Based on statements 1-5, "duck typing" is not specific to ruby, but rather made possible by the dynamic nature of a language. Based on statements 6-7, duck typing is neither specifically provided for nor compulsory, so it is not an attribute of the language. Thus, because it can be practiced, or not practiced; in ruby, or in other languages; it must be a _style_ available in dynamic languages, rather than a specific feature of a particular language. -- Ryan Pavlik "Damn ye and ye black ops mind games!" - 8BT