4 ms·
> no relation to OOP ‘classes’ I think functional programmers go too far in insisting how different functional programming is. A typeclass indeed is very simil
by c3534l 4y ago
> no relation to OOP ‘classes’
I think functional programmers go too far in insisting how different functional programming is. A typeclass indeed is very similar to an interface in object oriented languages and you can even have default implementations of functions and you can subclass a typeclass (that is, inherit). Its used to solve many of the exact same kinds of problems that OOP classes are. They are fundamentally similar beasts.
- bradrn 4y ago> A typeclass indeed is very similar to an interface in object oriented languages Agreed — as I mentioned in the post, typeclasses and interfaces are pretty similar. However, typeclasses and classes (i.e. the things used to make objects) are entirely different. I’ve seen numerous times when the similarity in names has caused immense confusion to people familiar with OOP, so I thought it would be worthwhile to clarify this. (As for similarities between OOP and FP in general, you may be interested in this article: https://www.parsonsmatt.org/2018/03/22/three_layer_haskell_cake.html https://www.parsonsmatt.org/2018/03/22/three_layer_haskell_c.... I endorse their conclusion that typeclasses can be used to implement something which in principle is basically OOP. Also, starting from a different point of view, I will note that Smalltalk code tends to be pretty functional in its structure.)
- js8 4y agoThey are different in important aspect - in OOP, conformance to an interface is defined where the class (new type) is defined, while conformance to a type class can be specified independently of type or type class definition. This is really important for interoperability. With type classes, I can say that type (class in OOP) from library A conforms to a type class (interface in OOP) in library B (without A knowing anything about B), which is impossible to do with interfaces.
- Dylan16807 4y ago> I can say that type (class in OOP) from library A conforms to a type class (interface in OOP) in library B Does that involve adding code to make it conform? Either way, there are languages based on classes and interfaces where you can do that.
- js8 4y agoYes, the point is, the code to make it conform is independent on both A and B. So no need to modify either library. And sure, we can discuss what OOP is, but I am using the most widely used definition of classes and interfaces, as understood by Java and .NET.