caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <martin_jambon@emailuser.net>
To: Matt Gushee <mgushee@havenrock.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] examples of heterogenous collections (containers ?)
Date: Wed, 31 Mar 2004 15:28:13 +0800 (HKT)	[thread overview]
Message-ID: <Pine.LNX.4.40.0403311417570.2547-100000@localhost> (raw)
In-Reply-To: <20040331054113.GC19538@swordfish>

On Tue, 30 Mar 2004, Matt Gushee wrote:

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

And to keep trace of the original object, we would use polymorphic
variants:

class virtual ['a] base_class =
object
  method virtual variant : 'a
end

class ['a] derived_class_A =
object (self)
  inherit ['a] base_class
  method variant = `A self
end

and ['a] derived_class_B =
object (self)
  inherit ['a] base_class
  method variant = `B self
end

type variant = [ `A of variant derived_class_A
	       | `B of variant derived_class_B ]

let a = new derived_class_A
let b = new derived_class_B
let l = [ (a :> variant base_class);
	  (b :> variant base_class) ]
let list_of_variants = List.map (fun o -> o#variant) l


But maybe it is more convenient to not define a variant method, just
build a list of specialized objects:
  let list_of_variants = [ `A obj1; `B obj2; `C anything; ... ]

and then:
  let l = List.map (function `A o | `B o | `C o -> (o :> base_class))

(but here you loose the reference to the specialized version)


Martin

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


      parent reply	other threads:[~2004-03-31  7:28 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
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 [this message]

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=Pine.LNX.4.40.0403311417570.2547-100000@localhost \
    --to=martin_jambon@emailuser.net \
    --cc=caml-list@inria.fr \
    --cc=mgushee@havenrock.com \
    /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).