From: briand@aracnet.com
To: skaller@users.sourceforge.net
Cc: Matt Gushee <mgushee@havenrock.com>, caml-list@pauillac.inria.fr
Subject: Re: [Caml-list] examples of heterogenous collections (containers ?)
Date: Wed, 31 Mar 2004 20:00:07 -0800 [thread overview]
Message-ID: <16491.37831.445677.611426@soggy.deldotd.com> (raw)
In-Reply-To: <1080723895.13269.143.camel@pelican.wigram>
>>>>> "skaller" == skaller <skaller@users.sourceforge.net> writes:
skaller> You are thinking about this problem *backwards*! :)
Well that's why it's giving me so much trouble. :-)
skaller> The cast will always work if the object has a method named
skaller> 'f' which when invoked with no arguments returns unit.
skaller> Obviously, because Ocaml is wonderful, your cast will fail
skaller> on objects without a suitable 'f' method.
I LIKE IT. No inheritance required. Objects which have the same
method with the same signature work "automagically".
skaller> The solution is to use a factory function in a single
skaller> module, and return an abstraction of the class type you
skaller> wish to deal with.
I'm not sure I followed that, can you expand ?
For future readers of the list here and appropriate example. It turns
out I was silly and never even bothered to look in the _manual_ of all
places. I looked everywhere else. This is covered in the section
called "Using Coercions", go figure.
Anyway, enough blather, here's the example :
class elt = object
method f x = 2 * x
end
;;
class elt_A = object
method f x = 3 *x
method g x = 3. *. x
end
;;
let the_list = [ new elt; new elt ; ((new elt_A) :> elt) ]
;;
List.iter (fun x -> print_int (x#f 2);) the_list
;;
It does the right thing. Here is a nice example to show type safety.
Re-define elt_A as:
class elt_A = object
method f x = 3. *. x
method g x = 3. *. x
end
And get the error :
This expression cannot be coerced to type elt = < f : int -> int >;
it has type elt_A = < f : float -> float; g : float -> float >
but is here used with type #elt as 'a = < f : int -> int; .. >
Thanks to all who answered.
Brian
-------------------
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
next prev parent reply other threads:[~2004-04-01 4:00 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 [this message]
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=16491.37831.445677.611426@soggy.deldotd.com \
--to=briand@aracnet.com \
--cc=caml-list@pauillac.inria.fr \
--cc=mgushee@havenrock.com \
--cc=skaller@users.sourceforge.net \
/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