5 ms·
I'm curious why you want interfaces in languages with duck typing.
by SpikeGronim 16y ago
I'm curious why you want interfaces in languages with duck typing.
- swannodette 16y agoBecause it allows programmers to define a minimum contract for behavior. Instead of having to subclass (and being implicated into a hierarchy) you can instead provide any object as long as it satisfies that interface.
- russellperry 16y agoAllowing a class-level contract would fundamentally alter the dynamic nature of a language like Ruby. It might be cool (although I personally don't see the benefit), but it wouldn't be Ruby. What mechanism would enforce the contract anyway? The best you could hope for would be a runtime error thrown when the interpreter detected the incompatibility, and you can do that manually now (I do it frequently to imitate abstract classes).
- rapind 16y agoclass IAnimal def growl raise end end
- russellperry 16y agoYeah exactly! 'raise "not implemented"' is how I imitate abstract classes (on the rare occasion that I need them).
- wrs 16y agoAren'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 ago