Posts Tagged ‘ruby’

Mixins vs. typeclasses

Sunday, November 22nd, 2009

This is a fragment of a discussion on polymorphism that I had in another context. I was pleased by how well this explanation came out, and wanted to put it in a more public format.

I’ve thought more about the issue you raised of whether Haskell’s typeclasses are the same as Ruby’s mixins, and I think my answer is a firm “sort of”. A Ruby mixin provides a consistent API and a consistent implementation to any class it’s added to. A Haskell typeclass provides a consistent API and an individual implementation to any type it’s defined for. It’s roughly the difference between base classes and interfaces in C++/C#/Java.