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 C3B9C7F7BA for ; Mon, 3 Feb 2014 11:36:10 +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.17.11; 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.17.11; 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.17.11; 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: AlECANFw71LU4xELnGdsb2JhbABZvGGFUoEFFg4BAQEBAQYNCQkUKIIlAQEFJxNPCxgJJQ8FKIgkARjCAR+JaxePEBaDDoEUBJgphjISjww X-IPAS-Result: AlECANFw71LU4xELnGdsb2JhbABZvGGFUoEFFg4BAQEBAQYNCQkUKIIlAQEFJxNPCxgJJQ8FKIgkARjCAR+JaxePEBaDDoEUBJgphjISjww X-IronPort-AV: E=Sophos;i="4.95,771,1384297200"; d="scan'208";a="56585916" Received: from mout.web.de ([212.227.17.11]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 03 Feb 2014 11:35:49 +0100 Received: from frosties.localnet ([37.49.32.119]) by smtp.web.de (mrweb102) with ESMTPSA (Nemesis) id 0MeBDG-1Vt9TP0MOs-00Prbm for ; Mon, 03 Feb 2014 11:35:49 +0100 Received: from mrvn by frosties.localnet with local (Exim 4.80) (envelope-from ) id 1WAGsW-0008CN-EZ for caml-list@inria.fr; Mon, 03 Feb 2014 11:35:48 +0100 Date: Mon, 3 Feb 2014 11:35:48 +0100 From: Goswin von Brederlow To: caml-list@inria.fr Message-ID: <20140203103548.GA31067@frosties> References: 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:xQrfLKH8KkFApABCQGU1p+SAqQxho3RKfvYmA6uXEdyl7eTEWew RIIFL118i86mVck7IjoxV9MGla/uFgoHFHIYMT9RbY3zBZmd0rB0M0T90F1t6MJwiUj++wi UNybl0WXmf40PCBzN2XLY0Tbk3QZIbIy7fSj9QOo7UIKo3Ph62sJUEelHNlYi9nJ6BMoSTc c8tJQCm4S6C0wz/Ah0EYQ== Subject: Re: [Caml-list] Question about objects and method overriding 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