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 7A7F87F7C2 for ; Mon, 3 Feb 2014 16:54:38 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=pra; client-ip=212.227.15.14; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=mailfrom; client-ip=212.227.15.14; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mout.web.de) identity=helo; client-ip=212.227.15.14; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="postmaster@mout.web.de"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlECAPS671LU4w8OnGdsb2JhbABZvFGFUoEKFg4BAQEBAQYNCQkUKIIlAQEFJxNPCxgJJQ8FKDSHcAEDFcMOHysiiR4XjjgBAVYWgw6BFASYKYYyEo8MgXA X-IPAS-Result: AlECAPS671LU4w8OnGdsb2JhbABZvFGFUoEKFg4BAQEBAQYNCQkUKIIlAQEFJxNPCxgJJQ8FKDSHcAEDFcMOHysiiR4XjjgBAVYWgw6BFASYKYYyEo8MgXA X-IronPort-AV: E=Sophos;i="4.95,773,1384297200"; d="scan'208";a="56648964" Received: from mout.web.de ([212.227.15.14]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 03 Feb 2014 16:54:38 +0100 Received: from frosties.localnet ([37.49.32.119]) by smtp.web.de (mrweb004) with ESMTPSA (Nemesis) id 0LyOuM-1VDO262Ke6-015nXz for ; Mon, 03 Feb 2014 16:54:36 +0100 Received: from mrvn by frosties.localnet with local (Exim 4.80) (envelope-from ) id 1WALr2-0000mn-2F for caml-list@inria.fr; Mon, 03 Feb 2014 16:54:36 +0100 Date: Mon, 3 Feb 2014 16:54:36 +0100 From: Goswin von Brederlow To: caml-list@inria.fr Message-ID: <20140203155435.GD2481@frosties> References: <20140203103548.GA31067@frosties> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Provags-ID: V03:K0:+7CcD6l5QsOFSx5I0h+cXLpJEJ7JiapIytWG1a2kuws27a/vsYo RrNm0Xvku3REE9V6fctPm0fRkZHmRjVEu3PpuRLGfHvBeCRfoHGxUG0IlFz8A1QRVThHYnC MX5Ja4rIN6oC2GPoyrPkqZfky9mAg0yQBkzyV8L3GJnAVtRECgVLE6ft5xQGIHEAwfUoQOs jWRaJnKPQGp7RiLPJr9GA== Subject: Re: [Caml-list] Question about objects and method overriding 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 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