English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] examples of heterogenous collections (containers ?)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-03-31 (05:41)
From: Matt Gushee <mgushee@h...>
Subject: Re: [Caml-list] examples of heterogenous collections (containers ?)
On Tue, Mar 30, 2004 at 08:55:36PM -0800, briand@aracnet.com wrote:
> I'm embarking on that most typical of ewexamples, the heterogenous list.
> So I have objects:
> base_class
>   virtual method
> derived_class_A
>   inherit base_class
> derived_class_B
>   inherit base_class
> ...
> And I would like to collect instances of derived_class_B,
> derived_class_B etc.. into a data structure.
> The obvious example is that I'd like to collect the objects into a
> list and do something like :
> List.iter 
>   (f obj ->
>     obj#method)
>   list_of_objs

This will work if all the classes have identical signatures. Of course,
that's not the case for most non-trivial examples.

> or is (obj :> base_class)#method ?

Probably not. First of all, you will need to cast the objects at the
point where you put them into a list together ... thus, at the point
where you do the method call, the cast has already been done. Second,
you don't necessarily want to cast them to the base class.

As some people never tire of pointing out ;-), inheritance is not 
subtyping. Actually, I haven't myself figured out all the implications
of that statement, nor learned the theoretical justification for it--but
at any rate, in OCaml class types are completely independent of
inheritance relationships. That's a good thing because it means you can
define a class type and arbitrarily apply it to any class that provides
*at least* the methods specified in the type signature, regardless of
inheritance. E.g.:

  class type x =
      method a : unit
      method b : int -> bool

... and so on.

> I've done quite a bit of searching but I cannot seem to find a "clean"
> example of doing the above, just bits and pieces.  Can anyone point me
> to any examples or code  ?

Hmm ... I recently wrote a text editor that uses a lot of class casts in
order to be able to manipulate groups of GUI objects that are instances
of different classes. I'm not sure it's a great learning example,
though. It works, but its code is hardly clean ... in fact it's easily
the ugliest thing I've done in OCaml. But if you can't find any better
examples, I could send you a tarball of it (it's not released yet).

Matt Gushee                 When a nation follows the Way,
Englewood, Colorado, USA    Horses bear manure through
mgushee@havenrock.com           its fields;
http://www.havenrock.com/   When a nation ignores the Way,
                            Horses bear soldiers through
                                its streets.
                            --Lao Tzu (Peter Merel, trans.)

To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners