I haven't had time to read through the whole article (and I already know how type classes work :)) so I just have one little point to add.
As correctly put in the article, type classes can provide default implementations. However, one cute thing that was missed is that type classes can provide default implementations for every function, referencing each other:
class Eq a where
(==), (/=) :: a -> a -> Bool
x /= y = not (x == y)
x == y = not (x /= y)
This way you can implement == or /=, and you get the other for free.
For me, the one new idea that made me understand why type classes are awesome was the read function: it's polymorphic on its return type. This means that you always give it a String and it returns whatever you need. This also means that you can have polymorphic constants--maxBound is the maximum of any bounded numeric type, so you can use it as an Int or a Float and it will always have the right value.
Overall, I now think that type classes are awesome. However, I also don't think a parallel to OOP is particularly helpful--I also learned OOP well before functional programming and found it easier to understand type classes by looking at how they were used than by comparing them to interfaces or templates.
Comments
I haven't had time to read through the whole article (and I already know how type classes work :)) so I just have one little point to add.
As correctly put in the article, type classes can provide default implementations. However, one cute thing that was missed is that type classes can provide default implementations for every function, referencing each other:
This way you can implement == or /=, and you get the other for free.For me, the one new idea that made me understand why type classes are awesome was the read function: it's polymorphic on its return type. This means that you always give it a String and it returns whatever you need. This also means that you can have polymorphic constants--maxBound is the maximum of any bounded numeric type, so you can use it as an Int or a Float and it will always have the right value.
Overall, I now think that type classes are awesome. However, I also don't think a parallel to OOP is particularly helpful--I also learned OOP well before functional programming and found it easier to understand type classes by looking at how they were used than by comparing them to interfaces or templates.