From: Peng Zang <peng.zang@gmail.com>
To: caml-list@yquem.inria.fr
Cc: Richard Jones <rich@annexia.org>, caml-list@inria.fr
Subject: Re: [Caml-list] Why can't immediate objects be extended?
Date: Sun, 13 Jul 2008 21:47:51 -0400 [thread overview]
Message-ID: <200807132147.55560.peng.zang@gmail.com> (raw)
In-Reply-To: <20080712141200.GA8634@annexia.org>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Saturday 12 July 2008 10:12:00 am Richard Jones wrote:
> I'm wondering if there's a reason why one cannot inherit from
> immediate objects? (See description at:
> http://caml.inria.fr/pub/docs/manual-ocaml/manual005.html#ss:immediate-obje
>cts) Is it just a syntax problem because the 'inherit' keyword currently
> needs to take a class type, or is there a deeper reason?
I have no idea, however, using some black magic we can accomplish exactly
this:
- ----------- snip of temp.ml -------------
(** ported from hash.c **)
let caml_hash_variant name =
let acc = ref 0 in
let len = String.length name in
for i = 0 to len - 1 do
acc := (223 * !acc + Char.code (String.get name i)) land 0x7FFFFFFF;
done;
!acc
;;
(** the return type is needs casting **)
let addmethod obj methodname methodfun =
let methodsuite = Obj.field obj 0 in
let newms =
let nummethods : int = Obj.magic (Obj.field methodsuite 0) in
let size = Obj.size methodsuite in
let arr = Array.create (size+2) 0 in
arr.(0) <- nummethods + 1;
for i = 1 to size - 1 do
arr.(i) <- Obj.magic (Obj.field methodsuite i);
done;
arr.(size) <- methodfun;
arr.(size+1) <- caml_hash_variant methodname;
arr in
let obj' = Obj.dup obj in
Obj.set_field obj' 0 (Obj.magic newms);
obj'
;;
let addmethod = Obj.magic addmethod
let addmethod : (< .. > as 'a) -> string -> ('a -> 'b) -> < .. > = addmethod;;
let foo = object method foo = "foo" end;;
let foobar : <foo:string; bar:string> = addmethod foo "bar" (fun _ -> "bar");;
foo#foo;;
foobar#foo;;
foobar#bar;;
- ----------- end snip ----------
Objective Caml version 3.09.3
# #use "temp.ml";;
val caml_hash_variant : string -> int = <fun>
val addmethod : Obj.t -> string -> int -> Obj.t = <fun>
val addmethod : 'a = <poly>
val addmethod : (< .. > as 'a) -> string -> ('a -> 'b) -> < .. > = <fun>
val foo : < foo : string > = <obj>
val foobar : < bar : string; foo : string > = <obj>
- - : string = "foo"
- - : string = "foo"
- - : string = "bar"
#
Not pretty, but it works,
Peng
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)
iD8DBQFIerBLfIRcEFL/JewRAnTpAJ9LdD09DsVvgJHemCf3ROzGqH+dQwCfRAoX
7sOvInXyvq8jDfF+ucZhzic=
=V3Ki
-----END PGP SIGNATURE-----
next prev parent reply other threads:[~2008-07-14 1:48 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-07-12 14:12 Richard Jones
2008-07-12 14:26 ` [Caml-list] " Richard Jones
2008-07-14 1:47 ` Peng Zang [this message]
2008-07-14 14:38 ` Keiko Nakata
2008-07-16 1:43 ` Jacques Garrigue
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=200807132147.55560.peng.zang@gmail.com \
--to=peng.zang@gmail.com \
--cc=caml-list@inria.fr \
--cc=caml-list@yquem.inria.fr \
--cc=rich@annexia.org \
/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