caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alain Frisch <alain@frisch.fr>
To: Goswin von Brederlow <goswin-v-b@web.de>, caml-list@inria.fr
Subject: Re: [Caml-list] Question about objects and method overriding
Date: Mon, 03 Feb 2014 14:55:35 +0100	[thread overview]
Message-ID: <52EF9FD7.7080204@frisch.fr> (raw)
In-Reply-To: <20140203103548.GA31067@frosties>

On 02/03/2014 11:35 AM, Goswin von Brederlow wrote:
> On the source level you can change a method by inheriting the old
> class and implementing the method again. But you seem to want to
> change the method at runtime. Problem there is that all instances of a
> class afaik have the same virtual table to dispatch methods. So
> changing a methong in one instance would change it in all.

Well, nothing prevent you from creating a fresh copy of the table for 
the new object, as in:

let unsafe_override (o : 'a) (ov : < .. >) : 'a =
   let o = Obj.repr (Oo.copy o) in
   let meths = Obj.dup (Obj.field o 0) in
   Obj.set_field o 0 meths;
   let nmeths : int = Obj.magic (Obj.field meths 0) in

   let ov = Obj.repr ov in
   let new_meths = Obj.field ov 0 in
   let nnew_meths : int = Obj.magic (Obj.field new_meths 0) in
   let i = ref 0 and j = ref 0 in
   while !i < nmeths && !j < nnew_meths do
     let tag : int = Obj.magic (Obj.field meths (3 + 2 * !i)) in
     let ntag : int = Obj.magic (Obj.field new_meths (3 + 2 * !j)) in
     if tag < ntag then incr i
     else if ntag < tag then incr j
     else begin
       let f : _ -> _ = Obj.magic (Obj.field new_meths (2 + 2 * !j)) in
       Obj.set_field meths (2 + 2 * !i) (Obj.magic (fun _ -> f ov));
       incr i;
       incr j
     end
   done;
   Obj.magic o


val unsafe_override : (< .. > as 'a) -> < .. > -> 'a = <fun>


This function returns a copy of its first argument (with the same type), 
where the methods which are also defined in the second argument are 
overridden with the implementation from the second argument (and when 
they are called, their "self" is the second argument).

This is type-unsafe because one should enforce that methods common to 
the two objects have the same type (and that there is no clash between 
the hash value of their other methods).  Is there a user-land hack to 
encode this property (or a stronger one, such as the type of the first 
argument be an extension of the type of the second argument)?

For a given list of methods, one can define safe (I believe!) instances 
of unsafe_override, for instance:

let override_x_y : (< x : 'x; y : 'y; .. > as 'this) -> < x : 'x; y : 'y 
 > -> 'this = unsafe_override;;


This function can be used to override methods x and y of an object 
having these methods and potentially more.

Some syntactic sugar could automate this a little bit.  For instance, 
with extension points, one could use a -ppx filter to rewrite:

  [#oo_override x y]

into:

(unsafe_override : (< x : 'x; y : 'y; .. > as 'this) -> < x : 'x; y : 'y 
 > -> 'this)

(with same more unique names for the generated type variables)




Alain

  parent reply	other threads:[~2014-02-03 13:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-02 11:55 Tom Ridge
2014-02-02 12:43 ` Jacques Garrigue
2014-02-02 12:50   ` Tom Ridge
2014-02-03 10:35 ` Goswin von Brederlow
2014-02-03 12:24   ` Tom Ridge
2014-02-03 15:54     ` Goswin von Brederlow
2014-02-03 13:55   ` Alain Frisch [this message]
2014-02-03 15:12     ` Tom Ridge
2014-02-05 18:22 ` remy

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=52EF9FD7.7080204@frisch.fr \
    --to=alain@frisch.fr \
    --cc=caml-list@inria.fr \
    --cc=goswin-v-b@web.de \
    /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).