In OOP static languages, interfaces are used to declare that several classes have some logical property - they are one-time, they can be compared with int , they can be serialized, etc.
Say .net did not have a standard IDisposable interface, and I just came up with this great idea:
interface IDiscardable { void Discard(); }
My application has a lot of System.Windows.Form s, and I think that a Form satisfies the logical requirements for IDiscardable . The problem is that Form is defined outside of my project, so C# (and Java , C++ ...) will not let me implement IDiscardable for it. C# does not allow me to formally imagine the fact that a Form can be discarded (and I would probably finish the MyForm wrapper MyForm or something else.
In contrast, Haskell has typeclasses that are logically similar to interfaces. A Show instance can be represented (or serialized) as a string, Eq allows you to compare, etc. But there is one important difference: you can write an instance of typeclass (which is similar to an interface implementation) without access to the source code of the type. Therefore, if Haskell provides me with some type of Form , the record of the Discardable instance is Discardable for it.
My question is: from the point of view of the language designer, is there any advantage for the first approach? Haskell not an object-oriented language - does the second approach violate OOP ?
Thanks!
c # oop programming-languages interface haskell
Benesh
source share