From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id EDDF67F7C2 for ; Mon, 3 Feb 2014 13:25:13 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of tom.j.ridge@googlemail.com) identity=pra; client-ip=209.85.160.54; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="tom.j.ridge@googlemail.com"; x-sender="tom.j.ridge@googlemail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of tom.j.ridge@googlemail.com designates 209.85.160.54 as permitted sender) identity=mailfrom; client-ip=209.85.160.54; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="tom.j.ridge@googlemail.com"; x-sender="tom.j.ridge@googlemail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-pb0-f54.google.com) identity=helo; client-ip=209.85.160.54; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="tom.j.ridge@googlemail.com"; x-sender="postmaster@mail-pb0-f54.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AksCAHuJ71LRVaA2lGdsb2JhbABZg0RXqkKURwgWDgEBAQEHCwsJEiqCJQEBBScZAQEsCwEPAQoLDQ0hIhIBBQEKEgYTEodeAQMRDZ5vixOEUgEFkzsKGScDChWJHhEGk0iYLoEyjwIYKYFlgnQ8 X-IPAS-Result: AksCAHuJ71LRVaA2lGdsb2JhbABZg0RXqkKURwgWDgEBAQEHCwsJEiqCJQEBBScZAQEsCwEPAQoLDQ0hIhIBBQEKEgYTEodeAQMRDZ5vixOEUgEFkzsKGScDChWJHhEGk0iYLoEyjwIYKYFlgnQ8 X-IronPort-AV: E=Sophos;i="4.95,772,1384297200"; d="scan'208";a="56604195" Received: from mail-pb0-f54.google.com ([209.85.160.54]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 03 Feb 2014 13:25:13 +0100 Received: by mail-pb0-f54.google.com with SMTP id uo5so6954084pbc.41 for ; Mon, 03 Feb 2014 04:25:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=fq7KQ6vi9XuKOHkVKzefBh1rqSI3RRJl4yBFus76Krw=; b=FQHKk+OktfMfbkH0VUf1nX2EJZP5Qr5DQcvDR/5ivjg0Xb5m8I7b1Jj4VXixaTW+Qu 40BX2U5sWsaqkfAi1+WEO6rFC1GEKVRF1k0JFN+fkecC6/MOOzH8mJGjvKbpUEk/YKog Ito9W2SZuRUEAbI9D6tXTrHSciBkYAWnU/7AduupCvijoEuYD4eFNH5KMy411R7oWIxU zMMrfpNNbI0J236h5STIYTCX9qR4dtxd88644XNkKbAetXjNQXb5XWUoIHO02NoLwmqH F9uyTnrspJbPSUJEni9Xmglp5qbQyjS00Ayh4aiW5gGDem8KVlTF79po3YrmqAKfEIny J8Yw== X-Received: by 10.68.34.37 with SMTP id w5mr2660698pbi.159.1391430311220; Mon, 03 Feb 2014 04:25:11 -0800 (PST) MIME-Version: 1.0 Sender: tom.j.ridge@googlemail.com Received: by 10.70.104.243 with HTTP; Mon, 3 Feb 2014 04:24:51 -0800 (PST) In-Reply-To: <20140203103548.GA31067@frosties> References: <20140203103548.GA31067@frosties> From: Tom Ridge Date: Mon, 3 Feb 2014 12:24:51 +0000 X-Google-Sender-Auth: fvA6YJxSsjl2OfKaxSZOE4ujmUY Message-ID: To: Goswin von Brederlow Cc: caml-list Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [Caml-list] Question about objects and method overriding 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 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 > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs