3 ms·
Would it be fair to say that typeclasses are a formalisation of ad-hoc polymorphism? With all the good and bad things that come with that.
by somewhereoutth 5y ago
Would it be fair to say that typeclasses are a formalisation of ad-hoc polymorphism? With all the good and bad things that come with that.
- dboreham 5y agoPretty much.
- mumblemumble 5y agoI may be way off on this, but I like to think of them as a bridge between ad-hoc and parametric polymorphism. Or maybe an ad hoc wolf in parametric sheep's clothing. Functions and types that refer to typeclasses are parametrically polymorphic insofar as they don't refer to a specific implementation of that type. This is quite different from run-of-the-mill function overloading, where you must supply separate implementations for every set of types you want to support. But, unlike how parametric polymorphism works in OOP, late binding is not an option. At the use site, the typeclass implementation you want to use must be statically resolvable. In that respect, it does behave like function overloading.
- rowanG077 5y agoMost of the pitfalls of ad-hoc polymorphism aren't possible with type classes. Unless you use some dangerous annotations.