5 ms·
But statically typed languages have duck typing too. Except it's called type inference. And F# has it.
by jganetsk 16y ago
But statically typed languages have duck typing too. Except it's called type inference. And F# has it.
- therockhead 16y agoI dont believe Duck typing and type inference are really the same thing.For duck typing the type is unimportant, it only cares about the operation, types matter for type inference. The behavior of structural typing in languages such as Scala are is closer to Duck typing IMHO.
- wcarss 16y agoThis is patently false. Duck typing: http://en.wikipedia.org/wiki/Duck_typing http://en.wikipedia.org/wiki/Duck_typing Type inference: http://en.wikipedia.org/wiki/Type_inference http://en.wikipedia.org/wiki/Type_inference Type inference relies upon known, preset interfaces and inheritance from other types. Duck typing relies solely upon the methods being used. In duck typing, this means that the user finds out at the time of use whether or not a method is supported by an object. Here, the "interface" to a type is not set in stone. It is inherently dynamic and as long as the message you pass is supported, (duck->quack), there isn't a problem. The mental jump which many people fail to make is to understand that Object Orientation is about message passing, not types. The key questions are, "what message are you sending", and "does the receiver have a response" - it got dogmatized somewhere in small-memory static-land long ago that the best or perhaps only way to tell if an object will respond is through static definitions of complete interfaces, which came to be very strongly associated with our conception of a "type". Once the compiler has used it to check interface constraints are unbroken, this information becomes useless and unchangeable. Ruby is an example of a language that does not use that means of answering the questions. Instead, an object has dynamic metadata about what messages it supports (specifically a hash of the method names), and uses that (malleable) data to generate an answer. It is fundamentally different than type inference in a statically typed language.
- prodigal_erik 16y agoDuck typing is a problem when it's done by message name and those names aren't distinguished by namespaces. We're already starting to hear horror stories of Ruby libraries that want the same name to mean different things, especially with monkey-patching.
- spooneybarger 16y agoMonkey patching has nothing to do with duck typing. Monkey patching is both incredibly powerful and exceedingly dangerous at the same time but, there is nothing about monkey patching that says you need duck typing. You do static typing AND have open classes. To pretend that problems related to open classes is somehow connected to duck typing is simply false.
- pbiggar 16y agoI wouldn't say that it has _nothing_ to do with monkey patching. Monkey patching is the ability to update classes members dynamically. Duck typing is the ability to update an object's members dynamically. Since the overall point is to dynamically create a new member of some object, I think the previous poster can be forgiven.
- spooneybarger 16y agoDuck typing isn't the ability to update an object's members dynamically. There is nothing inherent in the meaning of duck typing that says anything about updating. All it says if i ask: walk and talk of it and can walk and talk then as far as i'm concerned, its a duck because that is what a duck was expected to do. duck typing is polymorphism w/o inheritance.
- pbiggar 16y ago> walk and talk of it and can walk and talk then as far as i'm concerned, its a duck because that is what a duck was expected to do. This doesn't even parse. Duck-typing is the idea that an object's type at a particular time is made up of the set of fields and methods it has _at that time_. I suppose that doesn't strictly require updating, but it would be a pretty lame duck with it. Polymorphism w/o inheritance is a fine description. How do you think that can be accomplished without dynamic updating?
- Rusky 16y agoWhile duck typing is not type inference, you can still get the benefits of duck typing and real OO with structural typing, which can be done statically.