caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Question about objects and method overriding
Date: Mon, 3 Feb 2014 16:54:36 +0100	[thread overview]
Message-ID: <20140203155435.GD2481@frosties> (raw)
In-Reply-To: <CABooLwMsBp+rc0NT4jPPr6Sox1LiY8pyQbBK=VejmnSzE_zuNw@mail.gmail.com>

On Mon, Feb 03, 2014 at 12:24:51PM +0000, Tom Ridge wrote:
> The scenario might be: I don't have the ability to change the object
> implementation to use an instance variable (perhaps the object comes
> from a library I am using), or maybe I don't want to decide upfront
> when I create the object, which methods can be overridden in this way
> (i.e. I don't want to add explicit set_call methods for all functions
> that I might want to override).
> 
> 
> On 3 February 2014 10:35, Goswin von Brederlow <goswin-v-b@web.de> wrote:
> > On Sun, Feb 02, 2014 at 11:55:02AM +0000, Tom Ridge wrote:
> >> Dear caml-list,
> >>
> >> With records, one can functionally update a field e.g. as
> >>
> >> { r with some_field=new_value }
> >>
> >> And new_value may, of course, be a function.
> >>
> >> With objects, is there similar functionality? e.g. can I write something like
> >>
> >> {{ myobj with method some_method=new_method }}
> >>
> >> ?
> >>
> >> Of course, I could copy the methods from myobj explicitly into a new
> >> object (and set some_method to new_method), but I might not know all
> >> the methods available on myobj, and even if I do this becomes
> >> textually extremely verbose.
> >>
> >> Of course, new_method cannot directly refer to self etc. Basically I
> >> am using objects in a similar way to records, and would like to use
> >> this functional record update feature.
> >>
> >> Thanks
> >>
> >> Tom
> >
> > 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.
> >
> > But why not dispatch the method through a value of the insance using a
> > closure?
> >
> > class myobj = object
> >   val mutable call_fn = fun () -> ()
> >   method call = call_fn ()
> >   method set_call fn = call_fn <- fn
> > end
> >
> > MfG
> >         Goswin

class myobj = object
  inherit libobj as parent
  val mutable call_fn = fun () -> parent#call
  method call = call_fn ()
  method set_call fn = call_fn <- fn
end

You can always override the method of a given class.

But see other mail for the award deserving Obj.magic hack to change a
method on the fly.

MfG
	Goswin

  reply	other threads:[~2014-02-03 15:54 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 [this message]
2014-02-03 13:55   ` Alain Frisch
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=20140203155435.GD2481@frosties \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    /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).