4 ms·
Because it allows programmers to define a minimum contract for behavior. Instead of having to subclass (and being implicated into a hierarchy) you can instead p
by swannodette 16y ago
Because 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).