caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Matt Gushee <mgushee@havenrock.com>
To: caml-list@pauillac.inria.fr
Subject: Re: [Caml-list] examples of heterogenous collections (containers ?)
Date: Tue, 30 Mar 2004 22:41:14 -0700	[thread overview]
Message-ID: <20040331054113.GC19538@swordfish> (raw)
In-Reply-To: <16490.20296.852406.613011@soggy.deldotd.com>

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 =
    object
      method a : unit
      method b : int -> bool
    end

... 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


  reply	other threads:[~2004-03-31  5:41 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-31  4:55 briand
2004-03-31  5:41 ` Matt Gushee [this message]
2004-03-31  6:45   ` briand
2004-03-31  9:04     ` skaller
2004-04-01  4:00       ` briand
2004-04-01  5:38         ` Issac Trotts
2004-04-01  7:20         ` skaller
2004-03-31  7:28   ` Martin Jambon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040331054113.GC19538@swordfish \
    --to=mgushee@havenrock.com \
    --cc=caml-list@pauillac.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).