caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: briand@aracnet.com
Cc: Matt Gushee <mgushee@havenrock.com>, caml-list@pauillac.inria.fr
Subject: Re: [Caml-list] examples of heterogenous collections (containers ?)
Date: 31 Mar 2004 19:04:55 +1000	[thread overview]
Message-ID: <1080723895.13269.143.camel@pelican.wigram> (raw)
In-Reply-To: <16490.26870.331855.463270@soggy.deldotd.com>

On Wed, 2004-03-31 at 16:45, briand@aracnet.com wrote:

> So it seems to me that I need a "stronger" method to enforce safety,
> perhaps implementing x1 as a virtual method in base ?

You are thinking about this problem *backwards*! :)

Forget about how to declare the classes etc.

Focus on the list. You would like a list of some
type "elt" for which you can write:

	List.iter (fun x -> x#f) the_list;

Now define the type which 'the_list' must have.
Define the 'minimal' such type!

class type elt = object
	method f : unit
end

val the_list : elt list

That's it. All you need to do now is cast ANY object
to type "elt" and it will fit into your list.

The cast will always work if the object has a method
named 'f' which when invoked with no arguments returns unit.
Obviously, because Ocaml is wonderful, your cast will fail
on objects without a suitable 'f' method.

The KEY idea here is: the class types you need
for algorithms should be declared.

But forget the idea classes have types. They do,
but it isn't important. A class is just a function
which creates an object. Class types are *views*
of objects.

The class type of a class is the 'maximal' view
of an object which it constructs, and isn't 
really that important, in fact, it should probably
never be exposed in any interfaces because the full
class type will include data members due to
a problem needing to allocate space for them
when constructing an object.

The solution is to use a factory function in
a single module, and return an abstraction
of the class type you wish to deal with.


-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
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  9:05 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 [this message]
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=1080723895.13269.143.camel@pelican.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=briand@aracnet.com \
    --cc=caml-list@pauillac.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).