[Haskell-beginners] Re: Encapsulation and Polymorphism

Alex Rozenshteyn rpglover64 at gmail.com
Thu Aug 26 06:50:49 EDT 2010


>From what I understand (not much), It seems that type-classes are
more-or-less equivalent to OO interfaces (I think OP mentioned this), and
that containers with existential types are less more-or-less equivalent to
OO containers whose elements implement an interface, the biggest exception
being that you can't use anything that isn't a part of the class on the
contained values.

As I understand, something like this could be relevant:

> class AquaticLifeform a where
>   nutrition :: Int
>   reproduce :: Maybe (a, a)

On Thu, Aug 26, 2010 at 10:08 AM, Stephen Tetley
<stephen.tetley at gmail.com>wrote:

> Hi Drew
>
> Bear in mind though that existentials are not equivalent to subtyping in
> OO.
>
> For instance, with example 2.1 from [1] all you can do with an Obj is
> show it, so for the list xs all you can do is show the elements:
>
> data Obj = forall a. (Show a) => Obj a
>
> xs :: [Obj]
> xs = [Obj 1, Obj "foo", Obj 'c']
>
> Because Obj is an existential you can't do an case analysis on it - so
> you can't write a function like this:
>
> add_one_if_int (Obj (n::Int)) = Obj (n+1)
> add_one_if_int (Obj other)    = Obj other
>
> There really is nothing you can do with Obj other than show it.
>
>
> If you are trying to transliterate OO designs, you might quickly find
> existentials are too inert to be useful.
>
> Best wishes
>
> Stephen
>
> [1] http://www.haskell.org/haskellwiki/Existential_type
>
>
> On 26 August 2010 07:45, Drew Haven <drew.haven at gmail.com> wrote:
> > I think I found the answers to all my questions at
> > http://www.haskell.org/haskellwiki/Existential_type
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
          Alex R
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100826/94bfb9f3/attachment.html


More information about the Beginners mailing list