Hacker News new | past | comments | ask | show | jobs | submit login

The advantage you mention is one of many (over OOP interfaces), I'll repeat it as (A).

(A) Ability to instantiate old types with new classes

(B) Conditional instantiation: "Maybe a" is an instance of "Show" iff "a" is an instance of "Show"

(C) Can do return-type polymorphism, or polymorphism on any part of the signature

(D) Objects do not need to pay for the interface vtable pointer. Large array of some type-class instance can be much smaller than an array of an OOP interface subclass.

(E) When comparing two Ord instance values, there is only one Ord dictionary, rather than 2 to choose from. No false dilemma.




E) you do get a dilemma if you add two elements in a sorted structure , lexically in two different files, where two different Ord instances arr in scope, respectively. this function-is-instance problem is dual to OO's -object-is-instance problem.


A homogenous list is guaranteed to only have one instance unless you enable incoherent instances (a known bad extension) or confuse the compiler with orphan instances (though I've never encountered this and think it should be detectable at link time anyway).

One type -> at most one instance for any class, in Haskell.


(C) is covered by generics in most cases. The others, yes.


Generics aren't ad-hoc polymorphic. Type classes are.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: