From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p7RF6adG001384 for ; Sat, 27 Aug 2011 17:06:36 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtcBAD8HWU7U4366k2dsb2JhbABChEyjYBQBAQEBCQkLCRQDIoFAAQEEASMESwcQCw4MAiYCAlcGEwmHaQICqDaQf4EshA+BEQSMB4wmi3g X-IronPort-AV: E=Sophos;i="4.68,289,1312149600"; d="scan'208";a="117391703" Received: from moutng.kundenserver.de ([212.227.126.186]) by mail1-smtp-roc.national.inria.fr with ESMTP; 27 Aug 2011 17:06:31 +0200 Received: from office1.lan.sumadev.de (dslb-084-059-070-124.pools.arcor-ip.net [84.59.70.124]) by mrelayeu.kundenserver.de (node=mrbap4) with ESMTP (Nemesis) id 0LpNZt-1RT6J61zNN-00fC24; Sat, 27 Aug 2011 17:06:29 +0200 Received: from [192.168.1.111] (546BF154.cm-12-4d.dynamic.ziggo.nl [84.107.241.84]) by office1.lan.sumadev.de (Postfix) with ESMTPSA id 25E38C00C7; Sat, 27 Aug 2011 17:06:29 +0200 (CEST) From: Gerd Stolpmann To: Chris Yocum Cc: caml-list@inria.fr In-Reply-To: <4E58CCC3.4040901@gmail.com> References: <4E58CCC3.4040901@gmail.com> Content-Type: text/plain; charset="UTF-8" Date: Sat, 27 Aug 2011 17:06:28 +0200 Message-ID: <1314457588.3496.86.camel@thinkpad> Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:Ko851I6fvwI4HESdlpQwdfjKLS5xDtXsP0wHB5S7T+4 rF2OpdBAbONDGAXAw8QeUo8WDW4k70fqsj/EiETvDY+tB9ZpFD MuNzrzqAEwY7SwJYHaM60N4AkU8U/PuRRnW8Rh7yqFLUnfwGTx H+3SsYiweown4dFhjWR2InIePr7TPOhM4FIQG/qYCdMo7rEapj y/L+VDPFjcGVRdqlMiw7g== Subject: Re: [Caml-list] Ocaml and the Fragile Base Class Problem Am Samstag, den 27.08.2011, 11:53 +0100 schrieb Chris Yocum: > Hello, > > A friend of mine is giving a talk about "monkey patching" entitled > "Monkey patching, subclassing, and accidental overriding" > (http://aaroncrane.co.uk/talks/monkey_patching_subclassing/paper.html). > I was wondering how Ocaml deals with this situation or if it is even a > problem at all in Ocaml? I mocked up some code: > > class base = > object > method meth x = > print_endline "base"; > print_endline (string_of_int x) > end > > class deriv = > object > inherit base > method meth x = > print_endline "deriv"; > print_endline (string_of_int x) > end > > which kind of(?) shows the problem in Ocaml. He suggests in his paper > that using a Meta-Object Protocol is the way around this. What do you > think? I think it is a non-issue in practice. Simply because inheritance is quite seldom in Ocaml, even in programs using a lot of OO features. There is an attractive alternative, namely passing functions down to change the behavior of objects. So, you would normally define class base ?meth () = object method meth x = match meth with | None -> print_endline "base"; print_endline (string_of_int x) | Some f -> f x end class deriv = base ~meth:(fun x -> print_endline "deriv"; print_endline (string_of_int x) ) () You'd do something like this for all mutable behavior (maybe you wouldn't use individual functions for overriding, but some more elaborate data structure). Of course, this is generally a less powerful technique, but you often don't need the full power of inheritance. This is attractive for getting more control, and looks less like patching the algorithms of the base class. Also, you don't need inheritance in Ocaml for declaring subtyping relations. So, it is entirely possible to avoid the "inherit" keyword, and have nevertheless a lot of object-oriented design. Gerd > > Thanks, > Chris Yocum > -- ------------------------------------------------------------ Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de Creator of GODI and camlcity.org. Contact details: http://www.camlcity.org/contact.html Company homepage: http://www.gerd-stolpmann.de *** Searching for new projects! Need consulting for system *** programming in Ocaml? Gerd Stolpmann can help you. ------------------------------------------------------------