[Haskell-beginners] Typeclasses vs. Data

Felipe Almeida Lessa felipe.lessa at gmail.com
Thu Jul 21 17:37:03 CEST 2011


On Thu, Jul 21, 2011 at 12:22 PM, Thomas <haskell at phirho.com> wrote:
> The analogy between OO-interfaces and typeclasses seems to be a bit
> misleading here, though. Because IIUC then in Haskell typeclasses are not a
> substitute for a type - a misconception that bit me here, while in (at least
> the OO-languages I use) interfaces can usually be used instead of types
> (except object creation & assignment).

I prefer to say that the only similarity between OO classes and
typeclasses is the word "class".  Trying to make comparisons always
ends in tears.

> For example:
>    if n < 0
>        then (k :: X)
>        else (BeginCont k (n-1) :: BeginCont X)
> does not type check in Haskell.
> The "equivalent" construct would type check in OO(*) - although I understand
> that this does not mean the same in OO as in Haskell.

It is possible to do the same in Haskell using existentials.  But
forget that I've said that and don't try to use it.  Most of the time
you don't need them and there are better solutions.

Cheers, =)

-- 
Felipe.



More information about the Beginners mailing list