4 ms·
Aren't interfaces essentially a reification of duck typing? If it implements the duck interface, it's a duck. So the reason you'd want it is just to have a conv
by wrs 16y ago
Aren't interfaces essentially a reification of duck typing? If it implements the duck interface, it's a duck. So the reason you'd want it is just to have a conventional way to define what a duck is, and ask if an object was one. (And you could probably implement this in about three lines of Ruby.)
Now, many languages with formal interfaces (e.g., Java) are strongly typed, and they treat interfaces as types. Thus, for example, classes have to declare up front what interfaces they implement. That doesn't fit well with the flexible attitude of a duck-typed language. However, if interfaces were just collections of method signatures that could be tested against objects at runtime, that would be perfectly compatible with duck typing.
Languages like Modula-3 that use structural type equivalence sort of do this even in a strongly-typed system. Sort of.
- binaryfinery 16y agoSo in duck typing, a "duck interface" would just be a shortcut way of asking "is this thing I've got the kind of duck I want?" Is there a language like that? I would like that.
- gnaritas 16y agoThis presumes I ever want to ask an object what its type is, but that's generally not a good OO way to accomplish something. I'd rather tell the object what to do and assume it does the correct thing for its given type. If I must, I can always ask if it understands a given message, but that's to be avoided as well. Interfaces are useful in defining what an implementor must provide, but an example implementation does this as well.
- rapind 16y agothis. Also, if it helps, just think of the impl as an interface with a default behaviour that should be overriden ;) If this is too forgiving, then we can even have the impl methods throw errors ("override me!") and simulate an interface.
- gnaritas 16y agoIn Smalltalk, I can have all the methods throw someMethod self subclassResponsibility and then use the realize class refactoring on a subclass to generate stubs for all methods I must implement, just like an interface.
- theBobMcCormick 16y agoI think the Ruby way wouldn't be ask the object if it's a duck, but rather to ask it responds to the fly method, or the swim method, or maybe even the quack method, etc. In other words, you don't really care if it's a duck, just if it can accept the message you're about to send it.
- limmeau 16y agoSo you're checking if it implements an intersection of single-method ducktypes. Now if only there was a way to reify that intersection and give it a name...
- steveklabnik 16y ago... but this isn't done very often, and is generally considered a Bad Idea, so let's not encourage it with a special language construct.
- limmeau 16y agoWhat exactly is the bad idea here? Having more than one method in a duck-type?
- steveklabnik 16y agois_a? and respond_to? are code smells.
- limmeau 16y agoYes, I agree. I wasn't actually suggesting to sprinkle instanceof-checks throughout the code. What I was trying to get to: if you accept the existence of single-method ducktypes (like "things that have a walk() method" oder "things that have a quack() method"), then by intersecting these concepts, you arrive at things just like interfaces, whether you call them interfaces or not. If you want the pond system to work with your two-legged bird-sound robot, you perform the same mental steps as when implementing an interface. Hence, ducktypes could be seen as interfaces without an instanceof-operation.